-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathServiceExtensions.cs
146 lines (130 loc) · 6.09 KB
/
ServiceExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using QuantumCore.API.PluginTypes;
using QuantumCore.Networking;
using Serilog;
using Serilog.Events;
using Weikio.PluginFramework.Abstractions;
using Weikio.PluginFramework.Microsoft.DependencyInjection;
namespace QuantumCore.Extensions;
public static class ServiceExtensions
{
private const string MessageTemplate = "[{Timestamp:HH:mm:ss.fff}][{Level:u3}]{Message:lj} " +
"{NewLine:1}{Exception:1}";
/// <summary>
/// Used to register a packet provider per application type.
/// The application types might have duplicate packet definitions (by header) but they still might be handled
/// differently. Thus multiple packet providers may be registered if necessary with each registered as a keyed
/// service.
/// </summary>
/// <param name="services"></param>
/// <param name="mode"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IServiceCollection AddPacketProvider<T>(this IServiceCollection services, string mode)
where T : class, IPacketLocationProvider
{
services.AddKeyedSingleton<IPacketLocationProvider, T>(mode);
services.AddKeyedSingleton<IPacketReader, PacketReader>(mode);
services.AddKeyedSingleton<IPacketManager>(mode, (provider, key) =>
{
var packetLocationProvider = provider.GetRequiredKeyedService<IPacketLocationProvider>(key);
var assemblies = packetLocationProvider.GetPacketAssemblies();
var packetTypes = assemblies.SelectMany(x => x.ExportedTypes)
.Where(x => x.IsAssignableTo(typeof(IPacketSerializable)) &&
x.GetCustomAttribute<PacketAttribute>()?.Direction.HasFlag(EDirection.Incoming) == true)
.OrderBy(x => x.FullName)
.ToArray();
var handlerTypes = assemblies.SelectMany(x => x.ExportedTypes)
.Where(x =>
x.IsAssignableTo(typeof(IPacketHandler)) &&
x is {IsClass: true, IsAbstract: false, IsInterface: false})
.OrderBy(x => x.FullName)
.ToArray();
return ActivatorUtilities.CreateInstance<PacketManager>(provider,
new object[] {(IEnumerable<Type>) packetTypes, handlerTypes});
});
return services;
}
/// <summary>
/// Services required by Auth & Game
/// </summary>
/// <param name="services"></param>
/// <param name="pluginCatalog"></param>
/// <returns></returns>
public static IServiceCollection AddCoreServices(this IServiceCollection services, IPluginCatalog pluginCatalog,
IConfiguration configuration)
{
services.AddCustomLogging(configuration);
services.AddSingleton<IPacketManager>(provider =>
{
var packetTypes = AppDomain.CurrentDomain.GetAssemblies()
.Where(x => x.GetName().Name?.StartsWith("DynamicProxyGenAssembly") ==
false) // ignore Castle.Core proxies
.SelectMany(x => x.ExportedTypes)
.Where(x => x.IsAssignableTo(typeof(IPacketSerializable)) &&
x.GetCustomAttribute<PacketAttribute>()?.Direction.HasFlag(EDirection.Incoming) == true)
.OrderBy(x => x.FullName)
.ToArray();
var handlerTypes = AppDomain.CurrentDomain.GetAssemblies()
.Where(x => !x.IsDynamic)
.SelectMany(x => x.ExportedTypes)
.Where(x =>
x.IsAssignableTo(typeof(IPacketHandler)) &&
x is {IsClass: true, IsAbstract: false, IsInterface: false})
.OrderBy(x => x.FullName)
.ToArray();
return ActivatorUtilities.CreateInstance<PacketManager>(provider, [packetTypes, handlerTypes]);
});
services.AddSingleton<PluginExecutor>();
services.AddPluginFramework()
.AddPluginCatalog(pluginCatalog)
.AddPluginType<ISingletonPlugin>()
.AddPluginType<IConnectionLifetimeListener>()
.AddPluginType<IGameTickListener>()
.AddPluginType<IPacketOperationListener>()
.AddPluginType<IGameEntityLifetimeListener>();
return services;
}
private static IServiceCollection AddCustomLogging(this IServiceCollection services, IConfiguration configuration)
{
var config = new LoggerConfiguration();
// add minimum log level for the instances
config.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning);
// add destructuring for entities
config.Destructure.ToMaximumDepth(4)
.Destructure.ToMaximumCollectionCount(10)
.Destructure.ToMaximumStringLength(100);
// add environment variable
config.Enrich.WithEnvironmentUserName()
.Enrich.WithMachineName();
// add process information
config.Enrich.WithProcessId()
.Enrich.WithProcessName();
// add assembly information
// TODO: uncomment if needed
/* config.Enrich.WithAssemblyName() // {AssemblyName}
.Enrich.WithAssemblyVersion(true) // {AssemblyVersion}
.Enrich.WithAssemblyInformationalVersion(); */
// add exception information
config.Enrich.WithExceptionData();
// sink to console
config.WriteTo.Console(outputTemplate: MessageTemplate);
// sink to rolling file
config.WriteTo.RollingFile($"{Directory.GetCurrentDirectory()}/logs/api.log",
fileSizeLimitBytes: 10 * 1024 * 1024,
buffered: true,
outputTemplate: MessageTemplate);
config.ReadFrom.Configuration(configuration);
// finally, create the logger
services.AddLogging(x =>
{
x.ClearProviders();
x.AddSerilog(config.CreateLogger());
});
return services;
}
}