Esempio n. 1
0
        public static void Main(string[] args)
        {
            // Make our window nice and big
            Console.SetWindowSize(140, 36);
            LogLevel = Hybrasyl.Constants.DEFAULT_LOG_LEVEL;
            Assemblyinfo = new AssemblyInfo(Assembly.GetEntryAssembly());

            Constants.DataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Hybrasyl");

            if (!Directory.Exists(Constants.DataDirectory))
            {
                Logger.InfoFormat("Creating data directory {0}", Constants.DataDirectory);
                try
                {
                    // Create the various directories we need
                    Directory.CreateDirectory(Constants.DataDirectory);
                    Directory.CreateDirectory(Path.Combine(Constants.DataDirectory, "maps"));
                    Directory.CreateDirectory(Path.Combine(Constants.DataDirectory, "scripts"));

                }
                catch (Exception e)
                {
                    Logger.ErrorFormat("Can't create data directory: {0}", e.ToString());
                    return;
                }
            }
            
            var hybconfig = Path.Combine(Constants.DataDirectory, "config.xml");

            if (File.Exists(hybconfig))
            {
                var xml = File.ReadAllText(hybconfig);
                HybrasylConfig newConfig;
                Exception parseException;
                if (XML.Config.HybrasylConfig.Deserialize(xml, out newConfig, out parseException))
                    Config = newConfig;
                else
                {
                    Logger.ErrorFormat("Error parsing Hybrasyl configuration: {1}", hybconfig, parseException);
                    Environment.Exit(0);
                }
            }
            else
            {

                Console.ForegroundColor = ConsoleColor.White;

                Console.Write("Welcome to Project Hybrasyl: this is Hybrasyl server {0}\n", Assemblyinfo.Version);
                Console.Write("I need to ask some questions before we can go on. You'll also need to\n");
                Console.Write("make sure that an app.config exists in the Hybrasyl server directory,\n");
                Console.Write("and that the database specified there exists and is properly loaded.\n");
                Console.Write("Otherwise, you're gonna have a bad time.\n\n");
                Console.Write("These questions will only be asked once - if you need to make changes\n");
                Console.Write("in the future, edit config.xml in the Hybrasyl server directory.\n\n");

                Console.Write("Enter this server's IP address, or what IP we should bind to (default is 127.0.0.1): ");
                var serverIp = Console.ReadLine();
                Console.Write("Enter the Lobby Port (default is 2610): ");
                var lobbyPort = Console.ReadLine();
                Console.Write("Enter the Login Port: (default is 2611): ");
                var loginPort = Console.ReadLine();
                Console.Write("Enter the World Port (default is 2612): ");
                var worldPort = Console.ReadLine();

                if (String.IsNullOrEmpty(serverIp))
                    serverIp = "127.0.0.1";

                if (String.IsNullOrEmpty(lobbyPort))
                    lobbyPort = "2610";

                if (String.IsNullOrEmpty(loginPort))
                    loginPort = "2611";

                if (String.IsNullOrEmpty(worldPort))
                    worldPort = "2612";

                Logger.InfoFormat("Using {0}: {1}, {2}, {3}", serverIp, lobbyPort, loginPort, worldPort);

                Console.Write("Now, we will configure the Redis store.\n\n");
                Console.Write("Redis IP or hostname (default is localhost): ");
                var redisHost = Console.ReadLine();

                Console.Write("Redis authentication information (optional - if you don't have these, just hit enter)");
                Console.Write("Username: "******"Password: "******"localhost";

                Config = new HybrasylConfig {datastore = {host = redisHost}, network =
                {
                    lobby = new NetworkInfo { bindaddress = serverIp, port = Convert.ToUInt16(lobbyPort) },
                    login = new NetworkInfo { bindaddress = serverIp, port = Convert.ToUInt16(loginPort) },
                    world = new NetworkInfo { bindaddress = serverIp, port = Convert.ToUInt16(worldPort) }
                }};

                if (String.IsNullOrEmpty(redisUser))
                    Config.datastore.username = redisUser;

                if (String.IsNullOrEmpty(redisPass))
                    Config.datastore.username = redisPass;

                Config.SaveToFile(Path.Combine(Constants.DataDirectory, "config.xml"));
            }
            ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level = Level.Debug;
            ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(
                EventArgs.Empty);
            // Set console buffer, so we can scroll back a bunch
            Console.BufferHeight = Int16.MaxValue - 1;

            Logger.InfoFormat("Hybrasyl {0} starting.", Assemblyinfo.Version);
            Logger.InfoFormat("{0} - this program is licensed under the GNU AGPL, version 3.", Assemblyinfo.Copyright);

            LoadCollisions();

            // For right now we don't support binding to different addresses; the support in the XML
            // is for a distant future where that may be desirable.
            IpAddress = IPAddress.Parse(Config.network.lobby.bindaddress); 
            Lobby = new Lobby(Config.network.lobby.port);
            Login = new Login(Config.network.login.port);
            World = new World(Config.network.world.port, Config.datastore);
            World.InitWorld();

            byte[] addressBytes;
            addressBytes = IpAddress.GetAddressBytes();
            Array.Reverse(addressBytes);

            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream, Encoding.GetEncoding(949));

            writer.Write((byte)1);
            writer.Write((byte)1);
            writer.Write(addressBytes);
            writer.Write((byte)(2611 / 256));
            writer.Write((byte)(2611 % 256));
            writer.Write(Encoding.GetEncoding(949).GetBytes("Hybrasyl;Hybrasyl Production\0"));
            writer.Flush();

            var serverTable = stream.ToArray();
            ServerTableCrc = ~Crc32.Calculate(serverTable);
            ServerTable = Zlib.Compress(stream).ToArray();

            writer.Close();
            stream.Close();
            var serverMsg = Path.Combine(Constants.DataDirectory, "server.msg");
            
            if (File.Exists(serverMsg))
            {
                stream = new MemoryStream(Encoding.GetEncoding(949).GetBytes(File.ReadAllText(serverMsg)));

            }
            else
            {
                stream = new MemoryStream(Encoding.GetEncoding(949).GetBytes(String.Format("Welcome to Hybrasyl!\n\nThis is Hybrasyl (version {0}).\n\nFor more information please visit http://www.hybrasyl.com",
                    Assemblyinfo.Version)));
            }

            var notification = stream.ToArray();
            NotificationCrc = ~Crc32.Calculate(notification);
            Notification = Zlib.Compress(stream).ToArray();

            World.StartTimers();
            World.StartQueueConsumer();

            ToggleActive();

            while (true)
            {
                Lobby.AcceptConnection();
                Login.AcceptConnection();
                World.AcceptConnection();
                if (!IsActive())
                    break;
                Thread.Sleep(10);
                
            }
            Logger.Warn("Hybrasyl: all servers shutting down");
            // Server is shutting down. For Lobby and Login, this terminates the TCP listeners;
            // for World, this triggers a logoff for all logged in users and then terminates. After
            // termination, the queue consumer is stopped as well.
            // For a true restart we'll need to do a few other things; stop timers, etc.
            Lobby.Shutdown();
            Login.Shutdown();
            World.Shutdown();
            World.StopQueueConsumer();
            Logger.WarnFormat("Hybrasyl {0}: shutdown complete.", Assemblyinfo.Version);
            Environment.Exit(0);

        }
 public static bool LoadFromFile(string fileName, out HybrasylConfig obj)
 {
     System.Exception exception = null;
     return LoadFromFile(fileName, out obj, out exception);
 }
 public static bool Deserialize(string input, out HybrasylConfig obj)
 {
     System.Exception exception = null;
     return Deserialize(input, out obj, out exception);
 }
 /// <summary>
 /// Deserializes xml markup from file into an HybrasylConfig object
 /// </summary>
 /// <param name="fileName">string xml file to load and deserialize</param>
 /// <param name="obj">Output HybrasylConfig object</param>
 /// <param name="exception">output Exception value if deserialize failed</param>
 /// <returns>true if this Serializer can deserialize the object; otherwise, false</returns>
 public static bool LoadFromFile(string fileName, out HybrasylConfig obj, out System.Exception exception)
 {
     exception = null;
     obj = default(HybrasylConfig);
     try
     {
         obj = LoadFromFile(fileName);
         return true;
     }
     catch (System.Exception ex)
     {
         exception = ex;
         return false;
     }
 }
 /// <summary>
 /// Deserializes workflow markup into an HybrasylConfig object
 /// </summary>
 /// <param name="input">string workflow markup to deserialize</param>
 /// <param name="obj">Output HybrasylConfig object</param>
 /// <param name="exception">output Exception value if deserialize failed</param>
 /// <returns>true if this Serializer can deserialize the object; otherwise, false</returns>
 public static bool Deserialize(string input, out HybrasylConfig obj, out System.Exception exception)
 {
     exception = null;
     obj = default(HybrasylConfig);
     try
     {
         obj = Deserialize(input);
         return true;
     }
     catch (System.Exception ex)
     {
         exception = ex;
         return false;
     }
 }