-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
1,846 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/app/Edelstein.Application.Server/Bindings/StageConfigLogin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Edelstein.Protocol.Gameplay.Login; | ||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
|
||
namespace Edelstein.Application.Server.Bindings; | ||
|
||
public record StageConfigLogin : ILoginStageSystemOptions | ||
{ | ||
public string ID { get; init; } | ||
|
||
public string Host { get; init; } | ||
public int Port { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
using Edelstein.Common.Services.Server; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using System; | ||
using System.CommandLine; | ||
using System.IO; | ||
using Edelstein.Application.Server; | ||
|
||
var builder = Host.CreateApplicationBuilder(); | ||
var commandRoot = new RootCommand( | ||
"A mushroom game server emulator" | ||
); | ||
var argumentFile = new Argument<FileInfo>( | ||
"file or directory path", | ||
() => new FileInfo(AppDomain.CurrentDomain.BaseDirectory), | ||
"The file or directory path to stage json file(s)" | ||
); | ||
|
||
builder.Services.AddSerilog((_, logger) | ||
=> logger.ReadFrom.Configuration(builder.Configuration)); | ||
commandRoot.AddArgument(argumentFile); | ||
commandRoot.SetHandler(ProgramHandler.ExecuteRoot, argumentFile); | ||
|
||
builder.Services.AddDbContextFactory<ServerDbContext>(options | ||
=> options.UseNpgsql(builder.Configuration.GetConnectionString(ServerDbContext.ConnectionStringKey))); | ||
builder.Services.AddAutoMapper(typeof(ServerDbContext)); | ||
|
||
var host = builder.Build(); | ||
|
||
await host.RunAsync(); | ||
await commandRoot.InvokeAsync(args); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Edelstein.Application.Server.Bindings; | ||
using Edelstein.Common.Gameplay.Login; | ||
using Edelstein.Protocol.Gameplay.Login; | ||
using Edelstein.Protocol.Network.Transports; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
|
||
namespace Edelstein.Application.Server; | ||
|
||
internal static class ProgramHandler | ||
{ | ||
public async static Task ExecuteRoot(FileInfo file) | ||
{ | ||
var builder = Host.CreateApplicationBuilder(); | ||
|
||
builder.Services.AddSerilog((_, configuration) => configuration.ReadFrom.Configuration(builder.Configuration)); | ||
|
||
builder.Services.AddEdelsteinCommonUtilities(); | ||
builder.Services.AddEdelsteinCommonGameplay(); | ||
builder.Services.AddEdelsteinCommonGameplayLogin(); | ||
|
||
var configFileInfos = (file.Attributes & FileAttributes.Directory) != 0 | ||
? file.Directory?.GetFiles() ?? Array.Empty<FileInfo>() | ||
: new[]{ file }; | ||
|
||
foreach (var configFile in configFileInfos.Where(f => f.Extension == ".json")) | ||
{ | ||
var version = new TransportVersion(95, "1", 8); | ||
var config = new ConfigurationBuilder() | ||
.AddJsonFile(configFile.FullName, false, false) | ||
.Build(); | ||
|
||
switch (config["Type"]) | ||
{ | ||
case "Login": | ||
builder.Services.AddHostedService(p => | ||
{ | ||
var loginConfig = new StageConfigLogin(); | ||
var loginSystem = new LoginStageSystem(loginConfig); | ||
|
||
config.Bind(loginConfig); | ||
|
||
return new ServiceHostStage<ILoginStageUser, ILoginStageSystem>( | ||
p.GetRequiredService<ILogger<ServiceHostStage<ILoginStageUser, ILoginStageSystem>>>(), | ||
version, | ||
loginConfig, | ||
loginSystem | ||
); | ||
}); | ||
break; | ||
default: | ||
continue; | ||
} | ||
} | ||
|
||
var host = builder.Build(); | ||
|
||
await host.RunAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Edelstein.Common.Network.DotNetty.Transports; | ||
using Edelstein.Protocol.Gameplay; | ||
using Edelstein.Protocol.Network.Transports; | ||
using Edelstein.Protocol.Services.Server; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Edelstein.Application.Server; | ||
|
||
public class ServiceHostStage<TStageUser, TStageSystem>( | ||
ILogger<ServiceHostStage<TStageUser, TStageSystem>> logger, | ||
TransportVersion version, | ||
IServerEntry settings, | ||
IStageSystem<TStageUser, TStageSystem> system | ||
) : IHostedService | ||
where TStageUser : class, IStageUser<TStageUser, TStageSystem> | ||
where TStageSystem : IStageSystem<TStageUser, TStageSystem> | ||
{ | ||
private ITransportContext? Context { get; set; } | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var acceptor = new NettyTransportAcceptor<TStageUser>( | ||
version, | ||
system, | ||
system | ||
); | ||
|
||
Context = await acceptor.Accept(settings.Host, settings.Port); | ||
logger.LogInformation( | ||
"{ID} socket acceptor for v{Version}.{Patch} (Locale {Locale}) bound at {Host}:{Port}", | ||
settings.ID, | ||
version.Major, version.Patch, version.Locale, | ||
settings.Host, settings.Port | ||
); | ||
} | ||
|
||
public async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
logger.LogInformation( | ||
"{ID} socket acceptor shutting down, this may take awhile..", | ||
settings.ID | ||
); | ||
|
||
if (Context != null) | ||
await Context.Close(); | ||
|
||
logger.LogInformation( | ||
"{ID} socket acceptor finished shutting down", | ||
settings.ID | ||
); | ||
} | ||
} |
Oops, something went wrong.