Skip to content

Commit

Permalink
feat: Splits Server in Core and Application (#1806)
Browse files Browse the repository at this point in the history
> [!Note]
> **Developer Note**
> Now developers will only need to build/run the Application project instead of everything.
> When adding new projects, make sure to: 
> 1. Add a reference to that project in the Application project.
> 2. Add the dll file to the Distribution/Data/assemblies.json file

### Summary
- Adds an application project
- Consolidates process restarts to use `Core.Kill(true)`
- Removes some old messaging, for example processor optimization
- Fixes missing build cleanup
  • Loading branch information
kamronbatman committed May 31, 2024
1 parent e85c56e commit 60a3a6f
Show file tree
Hide file tree
Showing 43 changed files with 541 additions and 554 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Distribution Files
/Distribution/ModernUO
/Distribution/ModernUO.*
/Distribution/Server
/Distribution/Server.*
/Distribution/Assemblies
/Distribution/bsdtar
/Distribution/Configuration/antimacro.json
Expand All @@ -19,6 +21,7 @@
/Distribution/*.dylib
/Distribution/*.so
/Distribution/*.dll
/Distribution/*.exe
/Distribution/runtimes
/Distribution/nohup.out
/Distribution/ref
Expand Down
8 changes: 8 additions & 0 deletions ModernUO.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server.Tests", "Projects\Se
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UOContent.Tests", "Projects\UOContent.Tests\UOContent.Tests.csproj", "{3C4797F9-603E-44EF-8E8C-9275CC9EA74B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "Projects\Application\Application.csproj", "{E9849FA1-D4F5-4D68-A36B-249F4CB4E374}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Analyze|Any CPU = Analyze|Any CPU
Expand Down Expand Up @@ -41,6 +43,12 @@ Global
{3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Release|Any CPU.Build.0 = Release|Any CPU
{E9849FA1-D4F5-4D68-A36B-249F4CB4E374}.Analyze|Any CPU.ActiveCfg = Analyze|Any CPU
{E9849FA1-D4F5-4D68-A36B-249F4CB4E374}.Analyze|Any CPU.Build.0 = Analyze|Any CPU
{E9849FA1-D4F5-4D68-A36B-249F4CB4E374}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9849FA1-D4F5-4D68-A36B-249F4CB4E374}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9849FA1-D4F5-4D68-A36B-249F4CB4E374}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9849FA1-D4F5-4D68-A36B-249F4CB4E374}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
23 changes: 23 additions & 0 deletions Projects/Application/Application.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Diagnostics;
using System.Reflection;
using System.Threading;

namespace Server;

public class Application
{
public static void Main(string[] args)
{
bool profiling = false;

foreach (var a in args)
{
if (a.InsensitiveEquals("-profile"))
{
profiling = true;
}
}

Core.Setup(profiling, Assembly.GetEntryAssembly(), Process.GetCurrentProcess());
}
}
20 changes: 20 additions & 0 deletions Projects/Application/Application.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<ApplicationIcon>MUO.ico</ApplicationIcon>
<StartupObject>Server.Application</StartupObject>
<AssemblyName>ModernUO</AssemblyName>
<Win32Resource />
<Product>ModernUO Server</Product>
<OutDir>..\..\Distribution</OutDir>
<Version>0.0.0</Version>
<Configurations>Debug;Release;Analyze</Configurations>
<RootNamespace>Server</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Server\Server.csproj" />
<ProjectReference Include="..\UOContent\UOContent.csproj" Private="false" PrivateAssets="All" IncludeAssets="None">
<IncludeInPackage>false</IncludeInPackage>
</ProjectReference>
</ItemGroup>
</Project>
File renamed without changes.
6 changes: 4 additions & 2 deletions Projects/Server.Tests/Fixtures/ServerFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;

namespace Server.Tests;

Expand All @@ -8,14 +10,14 @@ internal class ServerFixture : IDisposable
// Global setup
static ServerFixture()
{
Core.Assembly = Assembly.GetExecutingAssembly(); // Server.Tests.dll
Core.ApplicationAssembly = Assembly.GetExecutingAssembly(); // Server.Tests.dll

// Load Configurations
ServerConfiguration.Load(true);

// Load an empty assembly list into the resolver
ServerConfiguration.AssemblyDirectories.Add(Core.BaseDirectory);
AssemblyHandler.LoadAssemblies(new[]{ "ModernUO.dll" });
AssemblyHandler.LoadAssemblies(["Server.dll"]);

Core.LoopContext = new EventLoopContext();
Core.Expansion = Expansion.EJ;
Expand Down
5 changes: 3 additions & 2 deletions Projects/Server.Tests/Server.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<IsPackable>false</IsPackable>
<Configurations>Debug;Release;Analyze</Configurations>
<RootNamespace>Server.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
Expand All @@ -10,9 +11,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<ProjectReference Include="..\Server\Server.csproj" />
<ProjectReference Include="..\UOContent\UOContent.csproj" />
<ProjectReference Include="..\Application\Application.csproj" />
<DataFiles Include="$(SolutionDir)\Distribution\Data\**" />
<ProjectReference Include="..\UOContent\UOContent.csproj" />
</ItemGroup>
<Target Name="CopyData" AfterTargets="AfterBuild">
<Copy SourceFiles="@(DataFiles)" DestinationFolder="$(OutDir)\Data\%(RecursiveDir)" />
Expand Down
97 changes: 36 additions & 61 deletions Projects/Server/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public static class Core
private static bool _profiling;
private static long _profileStart;
private static long _profileTime;
#nullable enable
private static bool? _isRunningFromXUnit;
#nullable restore

private static int _itemCount;
private static int _mobileCount;
Expand Down Expand Up @@ -114,6 +112,7 @@ public static bool Profiling
public static TimeSpan ProfileTime =>
TimeSpan.FromTicks(_profileStart > 0 ? _profileTime + (Stopwatch.GetTimestamp() - _profileStart) : _profileTime);

public static Assembly ApplicationAssembly { get; set; }
public static Assembly Assembly { get; set; }

// Assembly file version
Expand Down Expand Up @@ -177,10 +176,6 @@ public static DateTime Now

public static double AverageCPS => _cyclesPerSecond.Average();

public static bool MultiProcessor { get; private set; }

public static int ProcessorCount { get; private set; }

public static string BaseDirectory
{
get
Expand All @@ -189,7 +184,7 @@ public static string BaseDirectory
{
try
{
_baseDirectory = Assembly.Location;
_baseDirectory = ApplicationAssembly.Location;

if (_baseDirectory.Length > 0)
{
Expand Down Expand Up @@ -312,7 +307,7 @@ public static void Kill(bool restart = false)
_restartOnKill = restart;
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine(e.IsTerminating ? "Error:" : "Warning:");
Console.WriteLine(e.ExceptionObject);
Expand Down Expand Up @@ -378,23 +373,32 @@ internal static void DoKill(bool restart = false)

if (restart)
{
if (IsWindows)
{
Process.Start("dotnet", Assembly.Location);
}
else
try
{
var process = new Process
logger.Information("Restarting");
if (IsWindows)
{
Process.Start("dotnet", ApplicationAssembly.Location);
}
else
{
StartInfo = new ProcessStartInfo
var process = new Process
{
FileName = "dotnet",
Arguments = Assembly.Location,
UseShellExecute = true
}
};

process.Start();
StartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = ApplicationAssembly.Location,
UseShellExecute = true
}
};

process.Start();
}
logger.Information("Restart done");
}
catch (Exception e)
{
logger.Error(e, "Restart failed");
}
}

Expand All @@ -417,39 +421,22 @@ private static void HandleClosed()
}
}

public static void Main(string[] args)
public static void Setup(bool profiling, Assembly applicationAssembly, Process process)
{
Console.OutputEncoding = Encoding.UTF8;
Process = process;
ApplicationAssembly = applicationAssembly;
Assembly = Assembly.GetAssembly(typeof(Core));
Thread = Thread.CurrentThread;
LoopContext = new EventLoopContext();
SynchronizationContext.SetSynchronizationContext(LoopContext);
Profiling = profiling;

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
AppDomain.CurrentDomain.AssemblyResolve += AssemblyHandler.AssemblyResolver;

LoopContext = new EventLoopContext();

SynchronizationContext.SetSynchronizationContext(LoopContext);

foreach (var a in args)
{
if (a.InsensitiveEquals("-profile"))
{
Profiling = true;
}
}

Thread = Thread.CurrentThread;
Process = Process.GetCurrentProcess();
Assembly = Assembly.GetEntryAssembly();

if (Assembly == null)
{
throw new Exception("Assembly entry is missing.");
}

if (Thread != null)
{
Thread.Name = "Core Thread";
}
Console.OutputEncoding = Encoding.UTF8;
Thread.Name = "Core Thread";

if (BaseDirectory.Length > 0)
{
Expand All @@ -476,13 +463,6 @@ public static void Main(string[] args)
".TrimMultiline());
Utility.PopColor();

ProcessorCount = Environment.ProcessorCount;

if (ProcessorCount > 1)
{
MultiProcessor = true;
}

Console.CancelKeyPress += Console_CancelKeyPressed;

ServerConfiguration.Load();
Expand All @@ -496,11 +476,6 @@ public static void Main(string[] args)
logger.Information("Running with arguments: {Args}", s);
}

if (MultiProcessor)
{
logger.Information($"Optimizing for {{ProcessorCount}} processor{(ProcessorCount == 1 ? "" : "s")}", ProcessorCount);
}

var assemblyPath = Path.Join(BaseDirectory, AssembliesConfiguration);

// Load UOContent.dll
Expand Down
10 changes: 4 additions & 6 deletions Projects/Server/Server.csproj
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<PropertyGroup>
<OutputType>Exe</OutputType>
<ApplicationIcon>MUO.ico</ApplicationIcon>
<StartupObject>Server.Core</StartupObject>
<AssemblyName>ModernUO</AssemblyName>
<Win32Resource />
<Product>ModernUO Server</Product>
<Product>ModernUO Core</Product>
<OutDir>..\..\Distribution</OutDir>
<Version>0.0.0</Version>
<Configurations>Debug;Release;Analyze</Configurations>
<RootNamespace>Server</RootNamespace>
<PackageId>Server</PackageId>
</PropertyGroup>
<Target Name="CleanPub" AfterTargets="Clean">
<Message Text="Removing distribution files..." />
Expand All @@ -32,6 +29,7 @@
<Delete Files="..\..\Distribution\LibDeflate.Bindings.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\libdeflate.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\libdeflate.dylib" ContinueOnError="true" />
<Delete Files="..\..\Distribution\System.IO.Hashing.dll" ContinueOnError="true" />
</Target>
<ItemGroup>
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
Expand Down

0 comments on commit 60a3a6f

Please sign in to comment.