public static int Run(string[] args) { try { if (!HandleArguments(ref args)) { return(0); } } catch (OptionException e) { Console.Error.WriteLine(e.Message); return(1); } AppDomain.CurrentDomain.ProcessExit += ProcessExit; Console.CancelKeyPress += CancelKeyPress; ConsoleUtility.AddConsoleEventHandler(ConsoleEvent); Log.Level = Configuration.LogLevel; Log.TimestampFormat = Configuration.LogTimestampFormat; foreach (var src in Configuration.DiscardLogSources) { Log.DiscardSources.Add(src); } if (Configuration.Loggers.Contains(ConsoleLogger.Name)) { Log.Loggers.Add(new ConsoleLogger( Configuration.ColorsEnabled, Configuration.ErrorColor, Configuration.WarningColor, Configuration.BasicColor, Configuration.InfoColor, Configuration.DebugColor)); } if (Configuration.Loggers.Contains(FileLogger.Name)) { Log.Loggers.Add(new FileLogger( Configuration.LogDirectory, Configuration.LogFileNameFormat)); } if (!Debugger.IsAttached) { AppDomain.CurrentDomain.UnhandledException += UnhandledException; } _log.Basic("Starting {0}...", Name); var region = Configuration.Region; var sls = ServerListParameters.Uris[region]; var slsPort = Configuration.ServerListPort; if (slsPort == 0) { slsPort = sls.Port; } using var hostsMgr = Configuration.AdjustHostsFile ? new HostsFileManager() : null; using var certMgr = Configuration.AdjustCertificateStore && sls.Scheme == Uri.UriSchemeHttps ? new CertificateManager(slsPort) : null; var slsHost = sls.Host; var slsAddress = Configuration.ServerListAddress; hostsMgr?.RemoveEntry(slsHost, slsAddress); var real = Dns.GetHostEntry(slsHost).AddressList[0]; _log.Basic("Resolved official server list address: {0} -> {1}", slsHost, real); hostsMgr?.AddEntry(slsHost, slsAddress); var slsParams = new ServerListParameters(real, Configuration.ServerListAddress, slsPort, Configuration.GameAddress, Configuration.GameBasePort, region, Configuration.ServerListTimeout, Configuration.ServerListRetries); using var slsProxy = new ServerListProxy(slsParams); if (Configuration.ServerListEnabled) { slsProxy.Start(); } var pool = new ObjectPool <SocketAsyncEventArgs>( () => new SocketAsyncEventArgs(), x => x.Reset(), Configuration.PoolLimit != 0 ? (int?)Configuration.PoolLimit : null); var proc = new PacketProcessor(new CompilerPacketSerializer( new MessageTables(region, OpCodeTable.Versions[region]))); var proxies = slsProxy.Servers.Select(x => new GameProxy( x, pool, proc, Configuration.GameBacklog, Configuration.GameTimeout) { MaxClients = Configuration.GameMaxClients, }).ToArray(); foreach (var proxy in proxies) { proxy.Start(); } var loader = new PluginLoader(Configuration.PluginDirectory, Configuration.PluginPattern, Configuration.DisablePlugins); loader.Start(proxies); _log.Basic("{0} started", Name); _runningEvent.Wait(); _log.Basic("{0} shutting down...", Name); loader.Stop(proxies); foreach (var proxy in proxies) { proxy.Dispose(); } _exitEvent.Set(); return(0); }
public static int Run(string[] args) { Console.CancelKeyPress += CancelKeyPress; Log.Level = Configuration.LogLevel; Log.TimestampFormat = Configuration.LogTimestampFormat; foreach (var src in Configuration.DiscardLogSources) { Log.DiscardSources.Add(src); } if (Configuration.Loggers.Contains(ConsoleLogger.Name)) { Log.Loggers.Add(new ConsoleLogger( Configuration.EnableColors, Configuration.ErrorColor, Configuration.BasicColor, Configuration.InfoColor, Configuration.DebugColor)); } if (Configuration.Loggers.Contains(FileLogger.Name)) { Log.Loggers.Add(new FileLogger( Configuration.LogDirectory, Configuration.LogFileNameFormat)); } if (!Debugger.IsAttached) { AppDomain.CurrentDomain.UnhandledException += UnhandledException; } _log.Basic("Starting {0}...", Name); using (var hosts = Configuration.AdjustHostsFile ? new HostsFileManager() : null) { var region = Configuration.Region; var slsHost = ServerListParameters.GetHost(region); var slsAddress = Configuration.ServerListAddress; hosts?.RemoveEntry(slsHost, slsAddress); var real = Dns.GetHostEntry(slsHost).AddressList[0]; hosts?.AddEntry(slsHost, slsAddress); var slsParams = new ServerListParameters(real, Configuration.ServerListAddress, Configuration.GameAddress, region, Configuration.ServerListTimeout, Configuration.ServerListRetries); using (var slsProxy = new ServerListProxy(slsParams)) { var pool = new ObjectPool <SocketAsyncEventArgs>( () => new SocketAsyncEventArgs(), x => x.Reset(), Configuration.PoolLimit != 0 ? (int?)Configuration.PoolLimit : null); using (var writer = Configuration.EnablePacketLogs ? new PacketLogWriter(region, Configuration.PacketLogDirectory, Configuration.PacketLogFileNameFormat, Configuration.CompressPacketLogs) : null) { var proc = new PacketProcessor(new PacketSerializer( new OpCodeTable(true, region), new OpCodeTable(false, region)), writer); var proxies = slsProxy.Servers.Select(x => new GameProxy( x, pool, proc, Configuration.GameBacklog, Configuration.GameTimeout) { MaxClients = Configuration.GameMaxClients }).ToArray(); var loader = new PluginLoader(Configuration.PluginDirectory, Configuration.PluginPattern, Configuration.DisablePlugins); loader.Start(proxies); _log.Basic("{0} started", Name); _event.Wait(); _log.Basic("{0} shutting down...", Name); loader.Stop(proxies); foreach (var proxy in proxies) { proxy.Dispose(); } } } } return(0); }