Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This file is part of the OpenNos Emulator Project. See AUTHORS file for Copyright information
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- using log4net;
- using OpenNos.ChatLog.Networking;
- using OpenNos.Core;
- using OpenNos.DAL;
- using OpenNos.DAL.EF.Helpers;
- using OpenNos.Data;
- using System.Threading.Tasks;
- using OpenNos.GameObject;
- using OpenNos.Handler;
- using OpenNos.Master.Library.Client;
- using OpenNos.Master.Library.Data;
- using System;
- using System.Configuration;
- using System.Diagnostics;
- using System.Globalization;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using OpenNos.GameObject.Networking;
- namespace OpenNos.World
- {
- public static class Program
- {
- #region Members
- private static readonly ManualResetEvent _run = new ManualResetEvent(true);
- private static EventHandler _exitHandler;
- private static bool _isDebug;
- private static byte isGlacernon;
- private static bool _ignoreTelemetry;
- public static int _port;
- #endregion
- #region Delegates
- public delegate bool EventHandler(CtrlType sig);
- #endregion
- #region Enums
- public enum CtrlType
- {
- CTRL_C_EVENT = 0,
- CTRL_BREAK_EVENT = 1,
- CTRL_CLOSE_EVENT = 2,
- CTRL_LOGOFF_EVENT = 5,
- CTRL_SHUTDOWN_EVENT = 6
- }
- #endregion
- #region Methods
- public static void Main(string[] args)
- {
- #if DEBUG
- _isDebug = true;
- Thread.Sleep(1000);
- #endif
- bug:
- bool timeDone = false;
- Console.WriteLine("Is Glacernon? ( 0 / 1 ): ");
- if (isGlacernon != 0)
- {
- try
- {
- isGlacernon = Convert.ToByte(Console.ReadLine());
- }
- catch (Exception e)
- {
- goto bug;
- }
- }
- CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-US");
- Console.Title = $"OpenNos World Server{(_isDebug ? " Development Environment" : string.Empty)}";
- bool ignoreStartupMessages = false;
- bug2:
- isGlacernon = 0;
- if (isGlacernon == 0 || timeDone == true)
- {
- _port = Convert.ToInt32(ConfigurationManager.AppSettings["WorldPort"]);
- }
- else if (isGlacernon == 1)
- {
- _port = 5001;
- }
- int portArgIndex = Array.FindIndex(args, s => s == "--port");
- if (portArgIndex != -1
- && args.Length >= portArgIndex + 1
- && int.TryParse(args[portArgIndex + 1], out _port))
- {
- Console.WriteLine("Port override: " + _port);
- }
- foreach (string arg in args)
- {
- switch (arg)
- {
- case "--nomsg":
- ignoreStartupMessages = true;
- break;
- case "--notelemetry":
- _ignoreTelemetry = true;
- break;
- }
- }
- // initialize Logger
- Logger.InitializeLogger(LogManager.GetLogger(typeof(Program)));
- if (!ignoreStartupMessages)
- {
- Assembly assembly = Assembly.GetExecutingAssembly();
- FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
- string text = $"WORLD SERVER v{fileVersionInfo.ProductVersion}dev - PORT : {_port} by OpenNos Team";
- int offset = (Console.WindowWidth / 2) + (text.Length / 2);
- string separator = new string('=', Console.WindowWidth);
- Console.WriteLine(separator + string.Format("{0," + offset + "}\n", text) + separator);
- }
- // initialize api
- string authKey = ConfigurationManager.AppSettings["MasterAuthKey"];
- if (CommunicationServiceClient.Instance.Authenticate(authKey))
- {
- Logger.Info(Language.Instance.GetMessageFromKey("API_INITIALIZED"));
- }
- // initialize DB
- if (DataAccessHelper.Initialize())
- {
- // initialilize maps
- ServerManager.Instance.Initialize();
- }
- else
- {
- Console.ReadKey();
- return;
- }
- // TODO: initialize ClientLinkManager initialize PacketSerialization
- PacketFactory.Initialize<WalkPacket>();
- try
- {
- _exitHandler += ExitHandler;
- AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
- NativeMethods.SetConsoleCtrlHandler(_exitHandler, true);
- }
- catch (Exception ex)
- {
- Logger.Error("General Error", ex);
- }
- NetworkManager<WorldCryptography> networkManager = null;
- string ipAddress = ConfigurationManager.AppSettings["IPAddress"];
- portloop:
- try
- {
- networkManager = new NetworkManager<WorldCryptography>(ipAddress, _port, typeof(CommandPacketHandler), typeof(LoginCryptography), true);
- }
- catch (SocketException ex)
- {
- if (ex.ErrorCode == 10048)
- {
- _port++;
- Logger.Info("Port already in use! Incrementing...");
- goto portloop;
- }
- Logger.Error("General Error", ex);
- Environment.Exit(ex.ErrorCode);
- }
- ServerManager.Instance.ServerGroup = ConfigurationManager.AppSettings["ServerGroup"];
- const int sessionLimit = 100; // Needs workaround
- int? newChannelId = CommunicationServiceClient.Instance.RegisterWorldServer(new SerializableWorldServer(ServerManager.Instance.WorldId, ipAddress, _port, sessionLimit, ServerManager.Instance.ServerGroup));
- if (newChannelId.HasValue)
- {
- ServerManager.Instance.ChannelId = newChannelId.Value;
- MailServiceClient.Instance.Authenticate(authKey, ServerManager.Instance.WorldId);
- ConfigurationServiceClient.Instance.Authenticate(authKey, ServerManager.Instance.WorldId);
- ServerManager.Instance.Configuration = ConfigurationServiceClient.Instance.GetConfigurationObject();
- if (ServerManager.Instance.Configuration.UseChatLogService)
- {
- ChatLogServiceClient.Instance.Authenticate(ConfigurationManager.AppSettings["ChatLogKey"]);
- }
- ServerManager.Instance.MallApi = new GameObject.Helpers.MallAPIHelper(ServerManager.Instance.Configuration.MallBaseURL);
- }
- else
- {
- Logger.Error("Could not retrieve ChannelId from Web API.");
- Console.ReadKey();
- }
- }
- private static bool ExitHandler(CtrlType sig)
- {
- CommunicationServiceClient.Instance.UnregisterWorldServer(ServerManager.Instance.WorldId);
- ServerManager.Shout(string.Format(Language.Instance.GetMessageFromKey("SHUTDOWN_SEC"), 5));
- ServerManager.Instance.SaveAll();
- Thread.Sleep(5000);
- return false;
- }
- private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
- {
- ServerManager.Instance.InShutdown = true;
- Logger.Error((Exception)e.ExceptionObject);
- string pathd = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- using (System.IO.StreamWriter writer = System.IO.File.AppendText(pathd + @"\CRASHLOG.txt"))
- {
- writer.WriteLine((Exception)e.ExceptionObject);
- writer.Close();
- }
- try
- {
- if (!_ignoreTelemetry)
- {
- }
- }
- catch (Exception ex)
- {
- Logger.Error(ex);
- string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- using (System.IO.StreamWriter writer = System.IO.File.AppendText(path + @"\CRASHLOG.txt"))
- {
- writer.WriteLine(ex);
- }
- }
- Logger.Debug("Server crashed! Rebooting gracefully...");
- CommunicationServiceClient.Instance.UnregisterWorldServer(ServerManager.Instance.WorldId);
- ServerManager.Shout(string.Format(Language.Instance.GetMessageFromKey("SHUTDOWN_SEC"), 5));
- ServerManager.Instance.SaveAll();
- isGlacernon = 0;
- Process.Start("OpenNos.World.exe", $"--nomsg --port 1337");
- Environment.Exit(1);
- }
- #endregion
- #region Classes
- public static class NativeMethods
- {
- #region Methods
- [DllImport("Kernel32")]
- internal static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
- #endregion
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement