Skip to content

Commit

Permalink
Add NetScape Core
Browse files Browse the repository at this point in the history
  • Loading branch information
JayArrowz committed Jul 17, 2021
1 parent dbbee8c commit 281fbf8
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 109 deletions.
29 changes: 29 additions & 0 deletions NetScape.Core/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using NetScape.Abstractions.FileSystem;
using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace NetScape.Core
{
public class FileSystem : IFileSystem
{
private readonly FileSystemConfig _fileConfig;

public FileSystem(IConfigurationRoot configurationRoot)
{
_fileConfig = configurationRoot.GetSection("FileSystem").Get<FileSystemConfig>();
BasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
_fileConfig.BaseFolder);
}

public string BasePath { get; }

public string CachePath => Path.Combine(BasePath, _fileConfig.CacheFolder);
}

public class FileSystemConfig
{
public string BaseFolder { get; set; }
public string CacheFolder { get; set; }
}
}
30 changes: 30 additions & 0 deletions NetScape.Core/NetScape.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="Nuget.Projects.props" />
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>

<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Autofac" Version="6.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetScape.GameServer\NetScape.Modules.Server.csproj" />
<ProjectReference Include="..\NetScape.Modules.Cache\NetScape.Modules.Cache.csproj" />
<ProjectReference Include="..\NetScape.Modules.DAL\NetScape.Modules.DAL.csproj" />
<ProjectReference Include="..\NetScape.Modules.Logging.SeriLog\NetScape.Modules.Logging.SeriLog.csproj" />
<ProjectReference Include="..\NetScape.Modules.Region.Collision\NetScape.Modules.Region.Collision.csproj" />
<ProjectReference Include="..\NetScape.Modules.World\NetScape.Modules.World.csproj" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions NetScape.Core/Nuget.Projects.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project>
<PropertyGroup>
<IsPackable>true</IsPackable>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<RepositoryUrl>https://github.com/jayarrowz/NetScape</RepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<PackageTags>NetScape</PackageTags>
<PackageDescription>NetScape Core Libraries</PackageDescription>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<ItemGroup>
<None Include="../LICENSE.txt" Pack="true" PackagePath="LICENSE.txt"/>
</ItemGroup>
</Project>
89 changes: 89 additions & 0 deletions NetScape.Core/ServerHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NetScape.Abstractions.FileSystem;
using NetScape.Abstractions.Game;
using NetScape.Abstractions.Interfaces.IO;
using NetScape.Abstractions.Server;
using NetScape.Modules.Cache;
using NetScape.Modules.DAL;
using NetScape.Modules.Logging.SeriLog;
using NetScape.Modules.Region;
using NetScape.Modules.Region.Collision;
using NetScape.Modules.Server;
using NetScape.Modules.World;
using System;
using System.Collections.Generic;
using System.IO;

namespace NetScape.Core
{
public static class ServerHandler
{
public static ILifetimeScope RunServer(string configFileName, Action<DbContextOptionsBuilder, IConfigurationRoot> dbOptions, List<Module> modules)
{
var serviceCollection = new ServiceCollection();
var config = ConfigureServices(serviceCollection, configFileName, dbOptions);
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(serviceCollection);
ConfigureAutofac(containerBuilder, config, modules);
containerBuilder.RegisterBuildCallback(t => t.Resolve<ContainerProvider>().Container = (IContainer)t);
var container = containerBuilder.Build();
var serviceProvider = new AutofacServiceProvider(container);

ILifetimeScope scope = container.BeginLifetimeScope();
var gameServer = serviceProvider.GetRequiredService<IGameServer>();
_ = gameServer.BindAsync();
return scope;
}

public static void ConfigureCore(this ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot)
{
containerBuilder.RegisterModule(new SeriLogModule(configurationRoot));
containerBuilder.RegisterModule(new CacheModule());
containerBuilder.RegisterModule(new DALModule());
containerBuilder.RegisterModule(new GameServerModule(configurationRoot["BindAddr"], ushort.Parse(configurationRoot["BindPort"])));
containerBuilder.RegisterModule(new WorldModule());
containerBuilder.RegisterModule(new RegionModule());
containerBuilder.RegisterModule(new CollisionModule());
containerBuilder.RegisterType<WalkingQueueHandler>();
containerBuilder.RegisterType<FileSystem>().As<IFileSystem>();
containerBuilder.RegisterType<ContainerProvider>().SingleInstance();
}

private static void ConfigureAutofac(ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot, List<Module> modules)
{
foreach (var module in modules)
{
containerBuilder.RegisterModule(module);
}

containerBuilder.ConfigureCore(configurationRoot);
}

public static IConfigurationRoot ConfigureServices(this IServiceCollection serviceCollection, string configFileName, Action<DbContextOptionsBuilder, IConfigurationRoot> optionsAction)
{
var configurationRoot = CreateConfigurationRoot(configFileName);

// Add logging
serviceCollection.AddLogging();

//Build DB Connection
serviceCollection.AddDbContextFactory<DatabaseContext>(opts => optionsAction(opts, configurationRoot));

// Add access to generic IConfigurationRoot
serviceCollection.AddSingleton(configurationRoot);
return configurationRoot;
}

public static IConfigurationRoot CreateConfigurationRoot(string fileName)
{
return new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile(fileName, false)
.Build();
}
}
}
12 changes: 9 additions & 3 deletions NetScape.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetScape.Modules.ThreeOneSe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetScape.Modules.DAL.Test", "NetScape.Modules.DAL.Test\NetScape.Modules.DAL.Test.csproj", "{C663F250-D88B-4043-BD77-0EC27AD386AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetScape.Modules.FourSevenFour.LoginProtocol", "NetScape.Modules.FourSevenFour.LoginProtocol\NetScape.Modules.FourSevenFour.LoginProtocol.csproj", "{D111F2C7-6CE7-46BB-8624-5F032E3FA554}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetScape.Modules.FourSevenFour.LoginProtocol", "NetScape.Modules.FourSevenFour.LoginProtocol\NetScape.Modules.FourSevenFour.LoginProtocol.csproj", "{D111F2C7-6CE7-46BB-8624-5F032E3FA554}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetScape.Modules.FourSevenFour.Game", "NetScape.Modules.FourSevenFour.Game\NetScape.Modules.FourSevenFour.Game.csproj", "{60279A5D-B40C-4D58-9279-C85E07B7606D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetScape.Modules.FourSevenFour.Game", "NetScape.Modules.FourSevenFour.Game\NetScape.Modules.FourSevenFour.Game.csproj", "{60279A5D-B40C-4D58-9279-C85E07B7606D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetScape.Modules.FourSevenFour.World.Updating", "NetScape.Modules.FourSevenFour.World.Updating\NetScape.Modules.FourSevenFour.World.Updating.csproj", "{F644F124-9C5E-44BC-B483-E3114DD0B373}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetScape.Modules.FourSevenFour.World.Updating", "NetScape.Modules.FourSevenFour.World.Updating\NetScape.Modules.FourSevenFour.World.Updating.csproj", "{F644F124-9C5E-44BC-B483-E3114DD0B373}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetScape.Core", "NetScape.Core\NetScape.Core.csproj", "{8E4FE75A-0A37-432F-8936-900B18CE5835}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -126,6 +128,10 @@ Global
{F644F124-9C5E-44BC-B483-E3114DD0B373}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F644F124-9C5E-44BC-B483-E3114DD0B373}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F644F124-9C5E-44BC-B483-E3114DD0B373}.Release|Any CPU.Build.0 = Release|Any CPU
{8E4FE75A-0A37-432F-8936-900B18CE5835}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E4FE75A-0A37-432F-8936-900B18CE5835}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E4FE75A-0A37-432F-8936-900B18CE5835}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E4FE75A-0A37-432F-8936-900B18CE5835}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions NetScape/DesignTimeDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using NetScape.Core;
using NetScape.Modules.DAL;

namespace NetScape
Expand All @@ -9,9 +10,9 @@ public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<DatabaseCo
{
public DatabaseContext CreateDbContext(string[] args)
{
Kernel.SetConfigRoot();
var configRoot = ServerHandler.CreateConfigurationRoot("appsettings.json");
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
optionsBuilder.UseNpgsql(Kernel.ConfigurationRoot.GetConnectionString("NetScape"),
optionsBuilder.UseNpgsql(configRoot.GetConnectionString("NetScape"),
x => x.MigrationsAssembly(typeof(DatabaseContext)
.Assembly.GetName().Name));
return new DatabaseContext(optionsBuilder.Options);
Expand Down
15 changes: 0 additions & 15 deletions NetScape/FileSystem.cs

This file was deleted.

99 changes: 18 additions & 81 deletions NetScape/Kernel.cs
Original file line number Diff line number Diff line change
@@ -1,104 +1,41 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using System;
using Autofac;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NetScape.Abstractions.FileSystem;
using NetScape.Abstractions.Game;
using NetScape.Abstractions.Interfaces.IO;
using NetScape.Abstractions.Server;
using NetScape.Modules.Cache;
using NetScape.Core;
using NetScape.Modules.DAL;
using NetScape.Modules.Logging.SeriLog;
using NetScape.Modules.Messages;
using NetScape.Modules.Messages.Models;
using NetScape.Modules.Region;
using NetScape.Modules.Region.Collision;
using NetScape.Modules.Server;
using NetScape.Modules.ThreeOneSeven.Game;
using NetScape.Modules.ThreeOneSeven.LoginProtocol;
using NetScape.Modules.ThreeOneSeven.World.Updating;
using NetScape.Modules.World;
using System;
using System.IO;
using System.Collections.Generic;

namespace NetScape
{
public class Kernel
{
public static IConfigurationRoot ConfigurationRoot { get; set; }
public static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);

var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(serviceCollection);
ConfigureAutofac(containerBuilder, args);
containerBuilder.RegisterBuildCallback(t => t.Resolve<ContainerProvider>().Container = (IContainer)t);
var container = containerBuilder.Build();
var serviceProvider = new AutofacServiceProvider(container);

using (ILifetimeScope scope = container.BeginLifetimeScope())
List<Module> modules = new()
{
var gameServer = serviceProvider.GetRequiredService<IGameServer>();
_ = gameServer.BindAsync();

//TODO Make better
Console.ReadLine();
}
new ThreeOneSevenGameModule(),
new MessagesModule(
typeof(ThreeOneSevenEncoderMessages.Types),
typeof(ThreeOneSevenDecoderMessages.Types)
),
new ThreeOneSevenLoginModule(),
new ThreeOneSevenUpdatingModule()
};
ServerHandler.RunServer("appsettings.json", BuildDbOptions, modules);
Console.ReadLine();
}

private static void BuildDbOptions(DbContextOptionsBuilder optionsBuilder)
private static void BuildDbOptions(DbContextOptionsBuilder optionsBuilder, IConfigurationRoot configurationRoot)
{
optionsBuilder.UseNpgsql(ConfigurationRoot.GetConnectionString("NetScape"),
optionsBuilder.UseNpgsql(configurationRoot.GetConnectionString("NetScape"),
x => x.MigrationsAssembly(typeof(DatabaseContext)
.Assembly.GetName().Name));
.Assembly.FullName));
}

private static void ConfigureAutofac(ContainerBuilder containerBuilder, string[] args)
{
containerBuilder.RegisterModule(new ThreeOneSevenGameModule());
containerBuilder.RegisterModule(new MessagesModule(
typeof(ThreeOneSevenEncoderMessages.Types),
typeof(ThreeOneSevenDecoderMessages.Types))
);
containerBuilder.RegisterModule(new ThreeOneSevenLoginModule());
containerBuilder.RegisterModule(new ThreeOneSevenUpdatingModule());

containerBuilder.RegisterModule(new SeriLogModule(ConfigurationRoot));
containerBuilder.RegisterModule(new CacheModule());
containerBuilder.RegisterModule(new DALModule());
containerBuilder.RegisterModule(new GameServerModule(ConfigurationRoot["BindAddr"], ushort.Parse(ConfigurationRoot["BindPort"])));
containerBuilder.RegisterModule(new WorldModule());
containerBuilder.RegisterModule(new RegionModule());
containerBuilder.RegisterModule(new CollisionModule());
containerBuilder.RegisterType<WalkingQueueHandler>();
containerBuilder.RegisterType<FileSystem>().As<IFileSystem>();
containerBuilder.RegisterType<ContainerProvider>().SingleInstance();
}

public static void SetConfigRoot()
{
ConfigurationRoot = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", false)
.Build();
}

private static void ConfigureServices(IServiceCollection serviceCollection)
{
SetConfigRoot();

// Add logging
serviceCollection.AddLogging();

//Build DB Connection
serviceCollection.AddDbContextFactory<DatabaseContext>(BuildDbOptions);

// Add access to generic IConfigurationRoot
serviceCollection.AddSingleton(ConfigurationRoot);
}

}
}
9 changes: 1 addition & 8 deletions NetScape/NetScape.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,12 @@

<ItemGroup>
<ProjectReference Include="..\NetScape.Abstractions\NetScape.Abstractions.csproj" />
<ProjectReference Include="..\NetScape.GameServer\NetScape.Modules.Server.csproj" />
<ProjectReference Include="..\NetScape.Modules.DAL\NetScape.Modules.DAL.csproj" />
<ProjectReference Include="..\NetScape.Core\NetScape.Core.csproj" />
<ProjectReference Include="..\NetScape.Modules.FourSevenFour.Game\NetScape.Modules.FourSevenFour.Game.csproj" />
<ProjectReference Include="..\NetScape.Modules.FourSevenFour.LoginProtocol\NetScape.Modules.FourSevenFour.LoginProtocol.csproj" />
<ProjectReference Include="..\NetScape.Modules.FourSevenFour.World.Updating\NetScape.Modules.FourSevenFour.World.Updating.csproj" />
<ProjectReference Include="..\NetScape.Modules.Logging.SeriLog\NetScape.Modules.Logging.SeriLog.csproj" />
<ProjectReference Include="..\NetScape.Modules.Cache\NetScape.Modules.Cache.csproj" />
<ProjectReference Include="..\NetScape.Modules.Messages\NetScape.Modules.Messages.csproj" />
<ProjectReference Include="..\NetScape.Modules.Region.Collision\NetScape.Modules.Region.Collision.csproj" />
<ProjectReference Include="..\NetScape.Modules.Region\NetScape.Modules.Region.csproj" />
<ProjectReference Include="..\NetScape.Modules.ThreeOneSeven.Game\NetScape.Modules.ThreeOneSeven.Game.csproj" />
<ProjectReference Include="..\NetScape.Modules.ThreeOneSeven.LoginProtocol\NetScape.Modules.ThreeOneSeven.LoginProtocol.csproj" />
<ProjectReference Include="..\NetScape.Modules.ThreeOneSeven.World.Updating\NetScape.Modules.ThreeOneSeven.World.Updating.csproj" />
<ProjectReference Include="..\NetScape.Modules.World\NetScape.Modules.World.csproj" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions NetScape/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"BindAddr": "127.0.0.1",
"LoginProcessorTimeout": 10000,
"BindPort": 43594,
"FileSystem": {
"BaseFolder": "AspNetServerData/",
"CacheFolder": "cache/"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down

0 comments on commit 281fbf8

Please sign in to comment.