Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: converts client namespaces to file-scoped namespaces #2311

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
422 changes: 210 additions & 212 deletions Intersect.Client/Core/Audio.cs

Large diffs are not rendered by default.

128 changes: 63 additions & 65 deletions Intersect.Client/Core/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,93 +7,91 @@
using Intersect.Plugins.Contexts;
using Intersect.Plugins.Helpers;

namespace Intersect.Client.Core
namespace Intersect.Client.Core;


internal static partial class Bootstrapper
{
public static ClientContext Context { get; private set; }

internal static partial class Bootstrapper
public static void Start(params string[] args)
{
public static ClientContext Context { get; private set; }

public static void Start(params string[] args)
{
var parser = new Parser(
parserSettings =>
var parser = new Parser(
parserSettings =>
{
if (parserSettings == null)
{
if (parserSettings == null)
{
throw new ArgumentNullException(
nameof(parserSettings), @"If this is null the CommandLineParser dependency is likely broken."
);
}

parserSettings.AutoHelp = true;
parserSettings.AutoVersion = true;
parserSettings.IgnoreUnknownArguments = true;
throw new ArgumentNullException(
nameof(parserSettings), @"If this is null the CommandLineParser dependency is likely broken."
);
}
);

var logger = Log.Default;
var packetTypeRegistry = new PacketTypeRegistry(logger);
if (!packetTypeRegistry.TryRegisterBuiltIn())
{
logger.Error("Failed to register built-in packets.");
return;
parserSettings.AutoHelp = true;
parserSettings.AutoVersion = true;
parserSettings.IgnoreUnknownArguments = true;
}
);

var packetHandlerRegistry = new PacketHandlerRegistry(packetTypeRegistry, logger);
var packetHelper = new PacketHelper(packetTypeRegistry, packetHandlerRegistry);
FactoryRegistry<IPluginBootstrapContext>.RegisterFactory(PluginBootstrapContext.CreateFactory(args, parser, packetHelper));
var logger = Log.Default;
var packetTypeRegistry = new PacketTypeRegistry(logger);
if (!packetTypeRegistry.TryRegisterBuiltIn())
{
logger.Error("Failed to register built-in packets.");
return;
}

var commandLineOptions = parser.ParseArguments<ClientCommandLineOptions>(args)
.MapResult(HandleParsedArguments, HandleParserErrors);
var packetHandlerRegistry = new PacketHandlerRegistry(packetTypeRegistry, logger);
var packetHelper = new PacketHelper(packetTypeRegistry, packetHandlerRegistry);
FactoryRegistry<IPluginBootstrapContext>.RegisterFactory(PluginBootstrapContext.CreateFactory(args, parser, packetHelper));

if (!string.IsNullOrWhiteSpace(commandLineOptions.WorkingDirectory))
var commandLineOptions = parser.ParseArguments<ClientCommandLineOptions>(args)
.MapResult(HandleParsedArguments, HandleParserErrors);

if (!string.IsNullOrWhiteSpace(commandLineOptions.WorkingDirectory))
{
var workingDirectory = commandLineOptions.WorkingDirectory.Trim();
var resolvedWorkingDirectory = Path.GetFullPath(workingDirectory);
if (Directory.Exists(resolvedWorkingDirectory))
{
var workingDirectory = commandLineOptions.WorkingDirectory.Trim();
var resolvedWorkingDirectory = Path.GetFullPath(workingDirectory);
if (Directory.Exists(resolvedWorkingDirectory))
{
Environment.CurrentDirectory = resolvedWorkingDirectory;
}
else
{
Log.Warn($"Failed to set working directory to '{workingDirectory}', path does not exist: {resolvedWorkingDirectory}");
}
Environment.CurrentDirectory = resolvedWorkingDirectory;
}
else
{
Log.Warn($"Failed to set working directory to '{workingDirectory}', path does not exist: {resolvedWorkingDirectory}");
}

Context = new ClientContext(commandLineOptions, logger, packetHelper);
Context.Start();
}

private static ClientCommandLineOptions HandleParsedArguments(ClientCommandLineOptions clientCommandLineOptions) =>
clientCommandLineOptions;
Context = new ClientContext(commandLineOptions, logger, packetHelper);
Context.Start();
}

private static ClientCommandLineOptions HandleParserErrors(IEnumerable<Error> errors)
{
var errorsAsList = errors?.ToList();
private static ClientCommandLineOptions HandleParsedArguments(ClientCommandLineOptions clientCommandLineOptions) =>
clientCommandLineOptions;

var fatalParsingError = errorsAsList?.Any(error => error?.StopsProcessing ?? false) ?? false;
private static ClientCommandLineOptions HandleParserErrors(IEnumerable<Error> errors)
{
var errorsAsList = errors?.ToList();

var errorString = string.Join(
", ", errorsAsList?.ToList().Select(error => error?.ToString()) ?? Array.Empty<string>()
);
var fatalParsingError = errorsAsList?.Any(error => error?.StopsProcessing ?? false) ?? false;

var exception = new ArgumentException(
$@"Error parsing command line arguments, received the following errors: {errorString}"
);
var errorString = string.Join(
", ", errorsAsList?.ToList().Select(error => error?.ToString()) ?? Array.Empty<string>()
);

if (fatalParsingError)
{
Log.Error(exception);
}
else
{
Log.Warn(exception);
}
var exception = new ArgumentException(
$@"Error parsing command line arguments, received the following errors: {errorString}"
);

return default;
if (fatalParsingError)
{
Log.Error(exception);
}
else
{
Log.Warn(exception);
}

return default;
}

}
77 changes: 38 additions & 39 deletions Intersect.Client/Core/ClientCommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,45 @@
using Intersect.Client.Framework.Graphics;
using Intersect.Core;

namespace Intersect.Client.Core
namespace Intersect.Client.Core;

internal partial struct ClientCommandLineOptions : ICommandLineOptions
{
internal partial struct ClientCommandLineOptions : ICommandLineOptions
public ClientCommandLineOptions(
bool borderlessWindow,
int screenWidth,
int screenHeight,
string server,
string workingDirectory,
IEnumerable<string> pluginDirectories
)
{
public ClientCommandLineOptions(
bool borderlessWindow,
int screenWidth,
int screenHeight,
string server,
string workingDirectory,
IEnumerable<string> pluginDirectories
)
{
BorderlessWindow = borderlessWindow;
ScreenWidth = screenWidth;
ScreenHeight = screenHeight;
Server = server;
WorkingDirectory = workingDirectory;
PluginDirectories = pluginDirectories?.Select(Path.GetFullPath).ToArray();
}

[Option("borderless", Default = false, Required = false)]
public bool BorderlessWindow { get; }

[Option("screen-width", Default = 0, Required = false)]
public int ScreenWidth { get; }

[Option("screen-height", Default = 0, Required = false)]
public int ScreenHeight { get; }

[Option('S', "server", Default = null, Required = false)]
public string Server { get; }

[Option("working-directory", Default = null, Required = false)]
public string WorkingDirectory { get; }

[Option('p', "plugin-directory", Default = null, Required = false)]
public IEnumerable<string> PluginDirectories { get; }

public Resolution ScreenResolution => new Resolution(ScreenWidth, ScreenHeight);

BorderlessWindow = borderlessWindow;
ScreenWidth = screenWidth;
ScreenHeight = screenHeight;
Server = server;
WorkingDirectory = workingDirectory;
PluginDirectories = pluginDirectories?.Select(Path.GetFullPath).ToArray();
}

[Option("borderless", Default = false, Required = false)]
public bool BorderlessWindow { get; }

[Option("screen-width", Default = 0, Required = false)]
public int ScreenWidth { get; }

[Option("screen-height", Default = 0, Required = false)]
public int ScreenHeight { get; }

[Option('S', "server", Default = null, Required = false)]
public string Server { get; }

[Option("working-directory", Default = null, Required = false)]
public string WorkingDirectory { get; }

[Option('p', "plugin-directory", Default = null, Required = false)]
public IEnumerable<string> PluginDirectories { get; }

public Resolution ScreenResolution => new Resolution(ScreenWidth, ScreenHeight);

}
99 changes: 49 additions & 50 deletions Intersect.Client/Core/ClientContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,68 @@
using Intersect.Plugins.Interfaces;
using Intersect.Reflection;

namespace Intersect.Client.Core
namespace Intersect.Client.Core;

/// <summary>
/// Implements <see cref="IClientContext"/>.
/// </summary>
internal sealed partial class ClientContext : ApplicationContext<ClientContext, ClientCommandLineOptions>, IClientContext
{
/// <summary>
/// Implements <see cref="IClientContext"/>.
/// </summary>
internal sealed partial class ClientContext : ApplicationContext<ClientContext, ClientCommandLineOptions>, IClientContext
{
internal static bool IsSinglePlayer { get; set; }
internal static bool IsSinglePlayer { get; set; }

private IPlatformRunner mPlatformRunner;
private IPlatformRunner mPlatformRunner;

internal ClientContext(ClientCommandLineOptions startupOptions, Logger logger, IPacketHelper packetHelper) : base(
startupOptions, logger, packetHelper
)
{
FactoryRegistry<IPluginContext>.RegisterFactory(new ClientPluginContext.Factory());
}
internal ClientContext(ClientCommandLineOptions startupOptions, Logger logger, IPacketHelper packetHelper) : base(
startupOptions, logger, packetHelper
)
{
FactoryRegistry<IPluginContext>.RegisterFactory(new ClientPluginContext.Factory());
}

protected override bool UsesMainThread => true;
protected override bool UsesMainThread => true;

public IPlatformRunner PlatformRunner
{
get => mPlatformRunner ?? throw new ArgumentNullException(nameof(PlatformRunner));
private set => mPlatformRunner = value;
}
public IPlatformRunner PlatformRunner
{
get => mPlatformRunner ?? throw new ArgumentNullException(nameof(PlatformRunner));
private set => mPlatformRunner = value;
}

/// <inheritdoc />
protected override void InternalStart()
{
Networking.Network.PacketHandler = new PacketHandler(this, PacketHelper.HandlerRegistry);
PlatformRunner = typeof(ClientContext).Assembly.CreateInstanceOf<IPlatformRunner>();
PlatformRunner.Start(this, PostStartup);
}
/// <inheritdoc />
protected override void InternalStart()
{
Networking.Network.PacketHandler = new PacketHandler(this, PacketHelper.HandlerRegistry);
PlatformRunner = typeof(ClientContext).Assembly.CreateInstanceOf<IPlatformRunner>();
PlatformRunner.Start(this, PostStartup);
}

#region Exception Handling
#region Exception Handling

internal static void DispatchUnhandledException(
Exception exception,
bool isTerminating = true,
bool wait = false
)
{
var sender = Thread.CurrentThread;
var task = Task.Factory.StartNew(
() => HandleUnhandledException(sender, new UnhandledExceptionEventArgs(exception, isTerminating))
);
internal static void DispatchUnhandledException(
Exception exception,
bool isTerminating = true,
bool wait = false
)
{
var sender = Thread.CurrentThread;
var task = Task.Factory.StartNew(
() => HandleUnhandledException(sender, new UnhandledExceptionEventArgs(exception, isTerminating))
);

if (wait)
{
task.Wait();
}
if (wait)
{
task.Wait();
}
}

#endregion Exception Handling
#endregion Exception Handling

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing)
{
PacketHelper.HandlerRegistry.Dispose();
}
if (disposing)
{
PacketHelper.HandlerRegistry.Dispose();
}
}
}
Loading
Loading