Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            AppDomain.CurrentDomain.ProcessExit        += CurrentDomain_ProcessExit;

            ParseArguments(args);

            SetupConsoleLogging();

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

            if (m_Thread != null)
            {
                m_Thread.Name = "Core Thread";
            }

            if (BaseDirectory.Length > 0)
            {
                Directory.SetCurrentDirectory(BaseDirectory);
            }

            var version = Assembly.GetName().Version;

            CoreVersion = version;

            var platform = (int)Environment.OSVersion.Platform;

            if (platform == 4 || platform == 128)
            {
                Unix = true;
            }

            GCSettings.LatencyMode = GCLatencyMode.LowLatency;

            log.Info("X-RunUO Server - Version {0}.{1}.{2}, Build {3}", version.Major, version.Minor, version.Build, version.Revision);
            log.Info("Running on OS {0}", Environment.OSVersion);
            log.Info("Running on {0} {1}", Unix ? "Mono" : ".NET Framework", Environment.Version);

            if (MultiProcessor || Is64Bit)
            {
                log.Info("Optimizing for {0} {2}processor{1}", ProcessorCount, ProcessorCount == 1 ? "" : "s", Is64Bit ? "64-bit " : "");
            }

            log.Info("Using GC {0} {1} mode", GCSettings.IsServerGC ? "Server" : "Workstation", GCSettings.LatencyMode);

            Config = new RootConfig(BaseDirectory, "x-runuo.xml");

            Server.Config.Load();

            #region Dependency management
            LibraryConfig = new LibraryConfig(BaseDirectory, "libraries.xml");

            if (ForceUpdateDeps)
            {
                Directory.Delete(Path.Combine(BaseDirectory, "deps"), recursive: true);
            }
            #endregion

            if (!ScriptCompiler.Compile(Debug))
            {
                log.Fatal("Compilation failed. Press any key to exit.");
                Console.ReadLine();
                return;
            }

            ScriptCompiler.VerifyLibraries();

            // This instance is shared among timer scheduler and timer executor,
            // and accessed from both core & timer threads.
            var timerQueue = new Queue <Timer>();

            // Timer scheduler must be set up before world load, since world load
            // could schedule timers on entity deserialization.
            var timerScheduler = TimerScheduler.Instance = new TimerScheduler(timerQueue);
            m_TimerThread = new TimerThread(timerScheduler);

            var timerExecutor = new TimerExecutor(timerQueue);

            try
            {
                ScriptCompiler.Configure();

                TileData.Configure();
            }
            catch (TargetInvocationException e)
            {
                log.Fatal("Configure exception: {0}", e.InnerException);
                return;
            }

            SaveConfig();

            Region.Load();
            World.Load();

            try
            {
                ScriptCompiler.Initialize();
            }
            catch (TargetInvocationException e)
            {
                log.Fatal("Initialize exception: {0}", e.InnerException);
                return;
            }

            m_TimerThread.Start();

            NetServer netServer = new NetServer(new Listener(Listener.Port));
            netServer.Initialize();

            GameServer.Instance = new GameServer(netServer);
            GameServer.Instance.Initialize();

            EventSink.InvokeServerStarted();

            PacketDispatcher.Initialize();

            Now              = DateTime.UtcNow;
            m_TotalProfile   = new MainProfile(Now);
            m_CurrentProfile = new MainProfile(Now);

            try
            {
                while (!Closing)
                {
                    Now = DateTime.UtcNow;

                    Thread.Sleep(1);

                    ClockProfile(MainProfile.TimerId.Idle);

                    Mobile.ProcessDeltaQueue();

                    ClockProfile(MainProfile.TimerId.MobileDelta);

                    Item.ProcessDeltaQueue();

                    ClockProfile(MainProfile.TimerId.ItemDelta);

                    timerExecutor.Slice();

                    ClockProfile(MainProfile.TimerId.Timers);

                    netServer.Slice();

                    ClockProfile(MainProfile.TimerId.Network);

                    // Done with this iteration.
                    m_TotalProfile.Next();
                    m_CurrentProfile.Next();
                }
            }
            catch (Exception e)
            {
                HandleCrashed(e);
            }

            m_TimerThread.Stop();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            m_Assembly = Assembly.GetEntryAssembly();

            /* print a banner */
            Version ver = m_Assembly.GetName().Version;

            Console.WriteLine("SunUO Version {0}.{1}.{2} http://max.kellermann.name/projects/sunuo/",
                              ver.Major, ver.Minor, ver.Revision);
            Console.WriteLine("  on {0}, runtime {1}",
                              Environment.OSVersion, Environment.Version);

            if ((int)Environment.OSVersion.Platform == 128)
            {
                Console.WriteLine("Please make sure you have Mono 1.1.3 or newer! (mono -V)");
            }

            Console.WriteLine();

            /* prepare SunUO */
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            AppDomain.CurrentDomain.ProcessExit        += new EventHandler(CurrentDomain_ProcessExit);

            bool debug = false;

            for (int i = 0; i < args.Length; ++i)
            {
                if (Insensitive.Equals(args[i], "-debug"))
                {
                    debug = true;
                }
                else if (Insensitive.Equals(args[i], "-service"))
                {
                    m_Service = true;
                }
                else if (Insensitive.Equals(args[i], "-profile"))
                {
                    Profiling = true;
                }
            }

            config = new Config(Path.Combine(BaseDirectoryInfo.CreateSubdirectory("etc").FullName, "sunuo.xml"));

            try
            {
                m_MultiConOut = new MultiTextWriter(Console.Out);
                Console.SetOut(m_MultiConOut);

                if (m_Service)
                {
                    string filename = Path.Combine(LogDirectoryInfo.FullName, "console.log");
                    m_MultiConOut.Add(new FileLogger(filename));
                }
            }
            catch
            {
            }

            m_Thread  = Thread.CurrentThread;
            m_Process = Process.GetCurrentProcess();

            if (m_Thread != null)
            {
                m_Thread.Name = "Core Thread";
            }

            if (BaseDirectory.Length > 0)
            {
                Directory.SetCurrentDirectory(BaseDirectory);
            }

            Timer.TimerThread ttObj = new Timer.TimerThread();
            timerThread      = new Thread(new ThreadStart(ttObj.TimerMain));
            timerThread.Name = "Timer Thread";

            while (!ScriptCompiler.Compile(debug))
            {
                Console.WriteLine("Scripts: One or more scripts failed to compile or no script files were found.");
                Console.WriteLine(" - Press return to exit, or R to try again.");

                string line = Console.ReadLine();
                if (line == null || line.ToLower() != "r")
                {
                    return;
                }
            }

            Console.Write("Verifying scripts:");
            m_ItemCount   = 0;
            m_MobileCount = 0;
            foreach (Library l in ScriptCompiler.Libraries)
            {
                int itemCount = 0, mobileCount = 0;
                Console.Write(" {0}[", l.Name);
                l.Verify(ref itemCount, ref mobileCount);
                Console.Write("{0} items, {1} mobiles]", itemCount, mobileCount);
                m_ItemCount   += itemCount;
                m_MobileCount += mobileCount;
            }
            Console.WriteLine(" - done ({0} items, {1} mobiles)", m_ItemCount, m_MobileCount);

            try {
                ScriptCompiler.Configure();
            } catch (TargetInvocationException e) {
                Console.WriteLine("Configure exception: {0}", e.InnerException);
                return;
            }

            config.Save();

            World.Load();

            try {
                ScriptCompiler.Initialize();
            } catch (TargetInvocationException e) {
                Console.WriteLine("Initialize exception: {0}", e.InnerException);
                return;
            }

            Region.Load();

            MessagePump ms = m_MessagePump = new MessagePump(new Listener(Listener.Port));

            timerThread.Start();

            NetState.Initialize();

            EventSink.InvokeServerStarted();

            try
            {
                while (!m_Closing)
                {
                    Thread.Sleep(1);

                    Mobile.ProcessDeltaQueue();
                    Item.ProcessDeltaQueue();

                    Timer.Slice();
                    m_MessagePump.Slice();

                    NetState.FlushAll();
                    NetState.ProcessDisposedQueue();

                    if (Slice != null)
                    {
                        Slice();
                    }
                }
            }
            catch (Exception e)
            {
                CurrentDomain_UnhandledException(null, new UnhandledExceptionEventArgs(e, true));
            }

            if (timerThread.IsAlive)
            {
                timerThread.Abort();
            }
        }
Exemplo n.º 3
0
        public static void Start(bool repair)
        {
            if (!ScriptCompiler.Compile(true))
            {
                return;
            }

            m_ItemCount   = 0;
            m_MobileCount = 0;
            foreach (Library l in ScriptCompiler.Libraries)
            {
                int itemCount = 0, mobileCount = 0;
                l.Verify(ref itemCount, ref mobileCount);
                log.InfoFormat("Library {0} verified: {1} items, {2} mobiles",
                               l.Name, itemCount, mobileCount);
                m_ItemCount   += itemCount;
                m_MobileCount += mobileCount;
            }
            log.InfoFormat("All libraries verified: {0} items, {1} mobiles)",
                           m_ItemCount, m_MobileCount);

            try {
                TileData.Configure();

                ScriptCompiler.Configure();
            } catch (TargetInvocationException e) {
                log.Fatal("Configure exception: {0}", e.InnerException);
                return;
            }

            if (!config.Exists)
            {
                config.Save();
            }

            World.Load();
            if (World.LoadErrors > 0)
            {
                log.ErrorFormat("There were {0} errors during world load.", World.LoadErrors);
                if (repair)
                {
                    log.Error("The world load errors are being ignored for now, and will not reappear once you save this world.");
                }
                else
                {
                    log.Error("Try 'SunUO --repair' to repair this world save, or restore an older non-corrupt save.");
                    return;
                }
            }

            try {
                ScriptCompiler.Initialize();
            } catch (TargetInvocationException e) {
                log.Fatal("Initialize exception: {0}", e.InnerException);
                return;
            }

            Region.Load();

            m_MessagePump = new MessagePump();
            foreach (IPEndPoint ipep in Config.Network.Bind)
            {
                m_MessagePump.AddListener(new Listener(ipep));
            }

            Timer.TimerThread ttObj = new Timer.TimerThread();
            timerThread      = new Thread(new ThreadStart(ttObj.TimerMain));
            timerThread.Name = "Timer Thread";
            timerThread.Start();

            NetState.Initialize();
            Encryption.Initialize();

            EventSink.InvokeServerStarted();

            log.Info("SunUO initialized, entering main loop");

            try
            {
                Run();
            }
            catch (Exception e)
            {
                CurrentDomain_UnhandledException(null, new UnhandledExceptionEventArgs(e, true));
            }

            if (timerThread.IsAlive)
            {
                timerThread.Abort();
            }
        }
Exemplo n.º 4
0
        public static void Run()
        {
            EventSink.Instance = new EventSink();

            if (!ScriptCompiler.Compile(Environment.Debug))
            {
                Console.WriteLine("Fatal: Compilation failed. Press any key to exit.");
                Console.ReadLine();
                return;
            }

            ScriptCompiler.VerifyLibraries();

            // This instance is shared among timer scheduler and timer executor,
            // and accessed from both core & timer threads.
            Queue <Timer> timerQueue = new Queue <Timer>();

            // Timer scheduler must be set up before world load, since world load
            // could schedule timers on entity deserialization.
            var timerScheduler = TimerScheduler.Instance = new TimerScheduler(timerQueue);

            m_TimerThread = new TimerThread(timerScheduler);

            TimerExecutor timerExecutor = new TimerExecutor(timerQueue);

            PacketHandlers.Instance = new PacketHandlers();

            try
            {
                ScriptCompiler.Configure();

                TileData.Configure();
            }
            catch (TargetInvocationException e)
            {
                Console.WriteLine("Fatal: Configure exception: {0}", e.InnerException);
                return;
            }

            Environment.SaveConfig();

            Region.Load();
            World.Instance.Load();

            try
            {
                ScriptCompiler.Initialize();
            }
            catch (TargetInvocationException e)
            {
                Logger.Error("Initialize exception: {0}", e.InnerException);
                return;
            }

            m_TimerThread.Start();

            NetServer netServer = new NetServer(new Listener(Listener.Port));

            netServer.Initialize();

            GameServer.Instance = new GameServer(netServer, PacketHandlers.Instance);
            GameServer.Instance.Initialize();

            EventSink.Instance.InvokeServerStarted();

            PacketDispatcher.Initialize();

            m_Now            = DateTime.UtcNow;
            m_TotalProfile   = new MainProfile(m_Now);
            m_CurrentProfile = new MainProfile(m_Now);

            try
            {
                while (!m_Closing)
                {
                    m_Now = DateTime.UtcNow;

                    Thread.Sleep(1);

                    ClockProfile(MainProfile.TimerId.Idle);

                    Mobile.ProcessDeltaQueue();

                    ClockProfile(MainProfile.TimerId.MobileDelta);

                    Item.ProcessDeltaQueue();

                    ClockProfile(MainProfile.TimerId.ItemDelta);

                    timerExecutor.Slice();

                    ClockProfile(MainProfile.TimerId.Timers);

                    netServer.Slice();

                    ClockProfile(MainProfile.TimerId.Network);

                    // Done with this iteration.
                    m_TotalProfile.Next();
                    m_CurrentProfile.Next();
                }
            }
            catch (Exception e)
            {
                HandleCrashed(e);
            }

            m_TimerThread.Stop();
        }
Exemplo n.º 5
0
        public static void Start(Config.Root _config, bool debug, bool _service, bool _profiling)
        {
            config    = _config;
            m_Service = _service;
            Profiling = _profiling;

            m_Assembly = Assembly.GetEntryAssembly();

            /* prepare SunUO */
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            AppDomain.CurrentDomain.ProcessExit        += new EventHandler(CurrentDomain_ProcessExit);

            /* redirect Console to file in service mode */
            if (m_Service)
            {
                string     filename = Path.Combine(LogDirectoryInfo.FullName, "console.log");
                FileStream stream   = new FileStream(filename, FileMode.Create,
                                                     FileAccess.Write, FileShare.Read);
                StreamWriter writer = new StreamWriter(stream);
                Console.SetOut(writer);
                Console.SetError(writer);
            }

            m_Thread  = Thread.CurrentThread;
            m_Process = Process.GetCurrentProcess();

            if (m_Thread != null)
            {
                m_Thread.Name = "Core Thread";
            }

            if (BaseDirectory.Length > 0)
            {
                Directory.SetCurrentDirectory(BaseDirectory);
            }

            Timer.TimerThread ttObj = new Timer.TimerThread();
            timerThread      = new Thread(new ThreadStart(ttObj.TimerMain));
            timerThread.Name = "Timer Thread";

            if (!ScriptCompiler.Compile(debug))
            {
                return;
            }

            m_ItemCount   = 0;
            m_MobileCount = 0;
            foreach (Library l in ScriptCompiler.Libraries)
            {
                int itemCount = 0, mobileCount = 0;
                l.Verify(ref itemCount, ref mobileCount);
                log.Info(String.Format("Library {0} verified: {1} items, {2} mobiles",
                                       l.Name, itemCount, mobileCount));
                m_ItemCount   += itemCount;
                m_MobileCount += mobileCount;
            }
            log.Info(String.Format("All libraries verified: {0} items, {1} mobiles)",
                                   m_ItemCount, m_MobileCount));

            try {
                ScriptCompiler.Configure();
            } catch (TargetInvocationException e) {
                log.Fatal("Configure exception: {0}", e.InnerException);
                return;
            }

            if (!config.Exists)
            {
                config.Save();
            }

            World.Load();

            try {
                ScriptCompiler.Initialize();
            } catch (TargetInvocationException e) {
                log.Fatal("Initialize exception: {0}", e.InnerException);
                return;
            }

            Region.Load();

            m_MessagePump = new MessagePump(new Listener(Listener.Port));

            timerThread.Start();

            NetState.Initialize();
            Encryption.Initialize();

            EventSink.InvokeServerStarted();

            log.Info("SunUO initialized, entering main loop");

            try
            {
                while (!m_Closing)
                {
                    m_Signal.WaitOne();

                    m_Now = DateTime.Now;

                    Mobile.ProcessDeltaQueue();
                    Item.ProcessDeltaQueue();

                    Timer.Slice();
                    m_MessagePump.Slice();

                    NetState.FlushAll();
                    NetState.ProcessDisposedQueue();

                    if (Slice != null)
                    {
                        Slice();
                    }
                }
            }
            catch (Exception e)
            {
                CurrentDomain_UnhandledException(null, new UnhandledExceptionEventArgs(e, true));
            }

            if (timerThread.IsAlive)
            {
                timerThread.Abort();
            }
        }