public void Dispose() { if (m_IsDisposing) { return; } Dedicator.commandWindow.removeIOHandler(m_OpenModIoHandler); if (m_IoHandlers is not null) { m_IoHandlers.Reverse(); foreach (var ioHandler in m_IoHandlers) { Dedicator.commandWindow.addIOHandler(ioHandler); } m_IoHandlers.Clear(); } IsComponentAlive = false; m_IsDisposing = true; TlsWorkaround.Uninstalll(); m_Harmony?.UnpatchAll(OpenModComponentId); UnbindUnturnedEvents(); }
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 }
public void Dispose() { m_ShutdownBlock.Set(); TlsWorkaround.Uninstall(); UniTaskScheduler.UnobservedTaskException -= UniTaskExceptionHandler; UniTaskScheduler.DispatchUnityMainThread = true; Application.quitting -= OnApplicationQuitting; Console.CancelKeyPress -= OnCancelKeyPress; }
public void Dispose() { if (m_IsDisposing) { return; } IsComponentAlive = false; m_IsDisposing = true; TlsWorkaround.Uninstalll(); //m_Harmony.UnpatchAll(c_HarmonyInstanceId); UnbindUnturnedEvents(); }
public Task WaitForStartAsync(CancellationToken cancellationToken) { if (!PlayerLoopHelper.IsInjectedUniTaskPlayerLoop()) { var unitySynchronizationContextField = typeof(PlayerLoopHelper).GetField("unitySynchronizationContext", BindingFlags.Static | BindingFlags.NonPublic); // For older version of UniTask unitySynchronizationContextField ??= typeof(PlayerLoopHelper).GetField("unitySynchronizationContetext", BindingFlags.Static | BindingFlags.NonPublic) ?? throw new Exception("Could not find PlayerLoopHelper.unitySynchronizationContext field"); unitySynchronizationContextField.SetValue(null, SynchronizationContext.Current); var mainThreadIdField = typeof(PlayerLoopHelper).GetField("mainThreadId", BindingFlags.Static | BindingFlags.NonPublic) ?? throw new Exception("Could not find PlayerLoopHelper.mainThreadId field"); mainThreadIdField.SetValue(null, Thread.CurrentThread.ManagedThreadId); var playerLoop = PlayerLoop.GetCurrentPlayerLoop(); PlayerLoopHelper.Initialize(ref playerLoop); } // Handle UniTask exception UniTaskScheduler.UnobservedTaskException += UniTaskExceptionHandler; // Do not switch thread UniTaskScheduler.DispatchUnityMainThread = false; TlsWorkaround.Install(); Application.quitting += OnApplicationQuitting; Console.CancelKeyPress += OnCancelKeyPress; return(Task.CompletedTask); }
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 }