示例#1
0
        public Task InitAsync()
        {
            // ReSharper disable PossibleNullReferenceException
            IsComponentAlive = true;

            m_UnturnedCommandHandler.Subscribe();
            BindUnturnedEvents();

            var ioHandlers = (List <ICommandInputOutput>) typeof(CommandWindow)
                             .GetField("ioHandlers", BindingFlags.NonPublic | BindingFlags.Instance)
                             .GetValue(Dedicator.commandWindow);

            /* Fix Unturned destroying console and breaking Serilog formatting and colors */
            var shouldManageConsoleField = typeof(WindowsConsole).GetField("shouldManageConsole", BindingFlags.Static | BindingFlags.NonPublic);
            var shouldManageConsole      = (CommandLineFlag)shouldManageConsoleField.GetValue(null);

            shouldManageConsole.value = false;

            // unturned built-in io handlers
            var defaultIoHandlers = ioHandlers.Where(c => c.GetType() == typeof(ThreadedWindowsConsoleInputOutput) ||
                                                     c.GetType() == typeof(WindowsConsoleInputOutput) ||
                                                     c.GetType() == typeof(ThreadedConsoleInputOutput) ||
                                                     c.GetType() == typeof(ConsoleInputOutput)).ToList();

            foreach (var ioHandler in defaultIoHandlers)
            {
                Dedicator.commandWindow.removeIOHandler(ioHandler);
            }

            if (PlatformHelper.IsLinux)
            {
                Dedicator.commandWindow.addIOHandler(new SerilogConsoleInputOutput(m_LoggerFactory));
            }
            else
            {
                Dedicator.commandWindow.addIOHandler(new SerilogWindowsConsoleInputOutput(m_LoggerFactory));
            }

            m_Logger.LogInformation($"OpenMod for Unturned v{Version} is initializing...");

            m_Harmony.PatchAll(typeof(OpenModUnturnedHost).Assembly);
            TlsWorkaround.Install();

            var unitySynchronizationContetextField = typeof(PlayerLoopHelper).GetField("unitySynchronizationContetext", BindingFlags.Static | BindingFlags.NonPublic);

            unitySynchronizationContetextField.SetValue(null, SynchronizationContext.Current);

            var mainThreadIdField = typeof(PlayerLoopHelper).GetField("mainThreadId", BindingFlags.Static | BindingFlags.NonPublic);

            mainThreadIdField.SetValue(null, Thread.CurrentThread.ManagedThreadId);

            var playerLoop = PlayerLoop.GetDefaultPlayerLoop();

            PlayerLoopHelper.Initialize(ref playerLoop);

            m_Logger.LogInformation("OpenMod for Unturned is ready.");
            return(Task.CompletedTask);
            // ReSharper restore PossibleNullReferenceException
        }
示例#2
0
        public Task InitAsync()
        {
            // ReSharper disable PossibleNullReferenceException
            IsComponentAlive = true;

            m_Harmony = new Harmony(OpenModComponentId);
            m_Harmony.PatchAll(GetType().Assembly);

            m_UnturnedCommandHandler.Subscribe();
            BindUnturnedEvents();

            var ioHandlers = (List <ICommandInputOutput>) typeof(CommandWindow)
                             .GetField("ioHandlers", BindingFlags.NonPublic | BindingFlags.Instance)
                             .GetValue(Dedicator.commandWindow);

            CommandLineFlag shouldManageConsole = null;
            var             previousShouldManageConsoleValue = true;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                /* Fix Unturned destroying console and breaking Serilog formatting and colors */
                var windowsConsole           = typeof(Provider).Assembly.GetType("SDG.Unturned.WindowsConsole");
                var shouldManageConsoleField = windowsConsole?.GetField("shouldManageConsole", BindingFlags.Static | BindingFlags.NonPublic);

                if (shouldManageConsoleField != null)
                {
                    shouldManageConsole = (CommandLineFlag)shouldManageConsoleField.GetValue(null);
                    previousShouldManageConsoleValue = shouldManageConsole.value;
                    shouldManageConsole.value        = false;
                }
            }

            m_IoHandlers = ioHandlers.ToList(); // copy Unturneds IoHandlers
            // unturned built-in io handlers
            var defaultIoHandlers = ioHandlers.Where(c =>
                                                     c.GetType().FullName.Equals("SDG.Unturned.ThreadedWindowsConsoleInputOutput") || // type doesnt exist on Linux
                                                     c.GetType().FullName.Equals("SDG.Unturned.WindowsConsoleInputOutput") || // type doesnt exist on Linux
                                                     c.GetType() == typeof(ThreadedConsoleInputOutput) ||
                                                     c.GetType() == typeof(ConsoleInputOutput)).ToList();

            foreach (var ioHandler in defaultIoHandlers)
            {
                Dedicator.commandWindow.removeIOHandler(ioHandler);
            }

            if (shouldManageConsole != null)
            {
                shouldManageConsole.value = previousShouldManageConsoleValue;
            }

            m_OpenModIoHandler = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                ? ActivatorUtilities.CreateInstance <OpenModWindowsConsoleInputOutput>(m_ServiceProvider)
                : ActivatorUtilities.CreateInstance <OpenModConsoleInputOutput>(m_ServiceProvider);

            Dedicator.commandWindow.addIOHandler(m_OpenModIoHandler);

            m_Logger.LogInformation($"OpenMod for Unturned v{m_HostInformation.HostVersion} is initializing...");

            TlsWorkaround.Install();

            if (!s_UniTaskInited)
            {
                var unitySynchronizationContetextField =
                    typeof(PlayerLoopHelper).GetField("unitySynchronizationContetext",
                                                      BindingFlags.Static | BindingFlags.NonPublic);
                unitySynchronizationContetextField.SetValue(null, SynchronizationContext.Current);

                var mainThreadIdField =
                    typeof(PlayerLoopHelper).GetField("mainThreadId", BindingFlags.Static | BindingFlags.NonPublic);
                mainThreadIdField.SetValue(null, Thread.CurrentThread.ManagedThreadId);

                var playerLoop = PlayerLoop.GetDefaultPlayerLoop();
                PlayerLoopHelper.Initialize(ref playerLoop);
                s_UniTaskInited = true;
            }

            m_Logger.LogInformation("OpenMod for Unturned is ready.");

            BroadcastPlugins();
            return(Task.CompletedTask);
            // ReSharper restore PossibleNullReferenceException
        }