From 369a3217f794eba11bc4cc29902c2659daf75798 Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Sun, 10 Mar 2019 16:45:18 -0400 Subject: [PATCH 1/7] Change plugin update IO logic --- Torch.API/WebAPI/PluginQuery.cs | 10 +++++++++- Torch/Plugins/PluginManifest.cs | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Torch.API/WebAPI/PluginQuery.cs b/Torch.API/WebAPI/PluginQuery.cs index 5faf600..a13e5a8 100644 --- a/Torch.API/WebAPI/PluginQuery.cs +++ b/Torch.API/WebAPI/PluginQuery.cs @@ -95,6 +95,10 @@ namespace Torch.API.WebAPI try { path = path ?? $"Plugins\\{item.Name}.zip"; + string relpath = Path.GetDirectoryName(path); + + Directory.CreateDirectory(relpath); + var h = await _client.GetAsync(string.Format(PLUGIN_QUERY, item.ID)); string res = await h.Content.ReadAsStringAsync(); var response = JsonConvert.DeserializeObject(res); @@ -110,7 +114,11 @@ namespace Torch.API.WebAPI return false; } var s = await _client.GetStreamAsync(version.URL); - using (var f = new FileStream(path, FileMode.Create)) + + if(File.Exists(path)) + File.Delete(path); + + using (var f = File.Create(path)) { await s.CopyToAsync(f); await f.FlushAsync(); diff --git a/Torch/Plugins/PluginManifest.cs b/Torch/Plugins/PluginManifest.cs index 13618a9..7d1cc89 100644 --- a/Torch/Plugins/PluginManifest.cs +++ b/Torch/Plugins/PluginManifest.cs @@ -23,8 +23,12 @@ namespace Torch /// /// A GitHub repository in the format of Author/Repository to retrieve plugin updates. /// + [Obsolete("Updates no longer check git. Updates are hosted only on torchapi.net")] public string Repository { get; set; } + //xml tomfoolery + public bool ShouldSerializeRepository() => false; + /// /// The plugin version. This must include a string in the format of #[.#[.#]] for update checking purposes. /// From 6422f7c5764681d5bf006a4bdb3c40f42b6e67bf Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Tue, 12 Mar 2019 20:49:45 -0400 Subject: [PATCH 2/7] Fail *gracefully* when Havok runs out of memory. --- Torch/Patches/PhysicsMemoryPatch.cs | 54 +++++++++++++++++++++++++++++ Torch/Torch.csproj | 1 + 2 files changed, 55 insertions(+) create mode 100644 Torch/Patches/PhysicsMemoryPatch.cs diff --git a/Torch/Patches/PhysicsMemoryPatch.cs b/Torch/Patches/PhysicsMemoryPatch.cs new file mode 100644 index 0000000..bf94ad9 --- /dev/null +++ b/Torch/Patches/PhysicsMemoryPatch.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Havok; +using Sandbox; +using Sandbox.Engine.Physics; +using Sandbox.Game.World; +using Torch.Managers.PatchManager; +using Torch.Mod; +using Torch.Mod.Messages; +using VRage.Game; + +namespace Torch.Patches +{ + [PatchShim] + public static class PhysicsMemoryPatch + { + public static void Patch(PatchContext ctx) + { + ctx.GetPattern(typeof(MyPhysics).GetMethod("StepWorldsInternal", BindingFlags.NonPublic | BindingFlags.Instance)).Prefixes.Add(typeof(PhysicsMemoryPatch).GetMethod(nameof(PrefixPhysics))); + } + + public static bool NotifiedFailure { get; private set; } + + public static bool PrefixPhysics() + { + if (!HkBaseSystem.IsOutOfMemory) + return true; + + if (NotifiedFailure) + return false; + + NotifiedFailure = true; + ModCommunication.SendMessageToClients(new NotificationMessage("Havok has run out of memory. Server will restart in 30 seconds!", 60000, MyFontEnum.Red)); + //save the session NOW before anything moves due to weird physics. + MySession.Static.Save(); + //pause the game, for funsies + MySandboxGame.IsPaused = true; + + //nasty hack + Task.Run(() => + { + Thread.Sleep(TimeSpan.FromSeconds(30)); + TorchBase.Instance.Restart(); + }); + + return false; + } + } +} diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj index 928ddce..edc2f53 100644 --- a/Torch/Torch.csproj +++ b/Torch/Torch.csproj @@ -224,6 +224,7 @@ + From 2c47cfd60ee272088ec4691e69662db1aa74d715 Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Wed, 13 Mar 2019 14:41:25 -0400 Subject: [PATCH 3/7] Enable PatchShim for plugins --- Torch/TorchBase.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Torch/TorchBase.cs b/Torch/TorchBase.cs index 8f661f8..92f9964 100644 --- a/Torch/TorchBase.cs +++ b/Torch/TorchBase.cs @@ -485,6 +485,7 @@ namespace Torch if (_registeredAuxAssemblies.Add(asm)) { ReflectedManager.Process(asm); + PatchManager.AddPatchShims(asm); } } } From 7d8838c0ee244c4d9504512765e8028b67b9a5d1 Mon Sep 17 00:00:00 2001 From: John Gross Date: Mon, 8 Apr 2019 19:46:58 -0700 Subject: [PATCH 4/7] Add customizable server chat name and color --- Torch.API/ITorchConfig.cs | 2 ++ Torch.Server/TorchConfig.cs | 5 +++++ Torch.Server/Views/ChatControl.xaml | 4 ++-- Torch/Commands/CommandContext.cs | 13 ++++++++++--- Torch/Managers/ChatManager/ChatManagerClient.cs | 12 ++++++++++-- Torch/Utils/StringUtils.cs | 8 ++++++++ 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Torch.API/ITorchConfig.cs b/Torch.API/ITorchConfig.cs index 2b41621..308ee6b 100644 --- a/Torch.API/ITorchConfig.cs +++ b/Torch.API/ITorchConfig.cs @@ -18,6 +18,8 @@ namespace Torch bool ShouldUpdateTorch { get; } int TickTimeout { get; set; } string WaitForPID { get; set; } + string ChatName { get; set; } + string ChatColor { get; set; } bool Save(string path = null); } diff --git a/Torch.Server/TorchConfig.cs b/Torch.Server/TorchConfig.cs index 25a2452..e051d1e 100644 --- a/Torch.Server/TorchConfig.cs +++ b/Torch.Server/TorchConfig.cs @@ -5,6 +5,7 @@ using System.Windows; using System.Xml.Serialization; using Newtonsoft.Json; using NLog; +using VRage.Game; namespace Torch.Server { @@ -60,6 +61,10 @@ namespace Torch.Server /// public List Plugins { get; set; } = new List(); + public string ChatName { get; set; } = "Server"; + + public string ChatColor { get; set; } = "Red"; + public bool EnableWhitelist { get; set; } = false; public HashSet Whitelist { get; set; } = new HashSet(); diff --git a/Torch.Server/Views/ChatControl.xaml b/Torch.Server/Views/ChatControl.xaml index a5bc39a..79c1148 100644 --- a/Torch.Server/Views/ChatControl.xaml +++ b/Torch.Server/Views/ChatControl.xaml @@ -17,8 +17,8 @@ - - + + diff --git a/Torch/Commands/CommandContext.cs b/Torch/Commands/CommandContext.cs index 97a9203..c0c8532 100644 --- a/Torch/Commands/CommandContext.cs +++ b/Torch/Commands/CommandContext.cs @@ -55,10 +55,17 @@ namespace Torch.Commands Args = args ?? new List(); } - public virtual void Respond(string message, string sender = "Server", string font = MyFontEnum.Blue) + public virtual void Respond(string message, string sender = null, string font = MyFontEnum.Blue) { - Torch.CurrentSession.Managers.GetManager() - ?.SendMessageAsOther(sender, message, font, _steamIdSender); + var chat = Torch.CurrentSession.Managers.GetManager(); + if (sender != null) + { + chat?.SendMessageAsOther(sender, message, font, _steamIdSender); + } + else + { + chat?.SendMessageAsSelf(message); + } } } } \ No newline at end of file diff --git a/Torch/Managers/ChatManager/ChatManagerClient.cs b/Torch/Managers/ChatManager/ChatManagerClient.cs index 9c55455..5b0fbe4 100644 --- a/Torch/Managers/ChatManager/ChatManagerClient.cs +++ b/Torch/Managers/ChatManager/ChatManagerClient.cs @@ -38,10 +38,18 @@ namespace Torch.Managers.ChatManager { if (Sandbox.Engine.Platform.Game.IsDedicated) { + // Sending invalid color to clients will crash them. KEEEN + var color = Torch.Config.ChatColor; + if (!StringUtils.IsFontEnum(Torch.Config.ChatColor)) + { + _log.Warn("Invalid chat font color! Defaulting to 'Red'"); + color = MyFontEnum.Red; + } + var scripted = new ScriptedChatMsg() { - Author = "Server", - Font = MyFontEnum.Red, + Author = Torch.Config.ChatName, + Font = color, Text = message, Target = 0 }; diff --git a/Torch/Utils/StringUtils.cs b/Torch/Utils/StringUtils.cs index aade17e..167807a 100644 --- a/Torch/Utils/StringUtils.cs +++ b/Torch/Utils/StringUtils.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; +using VRage.Game; namespace Torch.Utils { @@ -60,5 +62,11 @@ namespace Torch.Utils } return builder?.ToString() ?? ""; } + + private static readonly string[] _fontEnumValues = typeof(MyFontEnum).GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => x.IsLiteral && !x.IsInitOnly).Select(x => (string)x.GetValue(null)).ToArray(); + public static bool IsFontEnum(string str) + { + return _fontEnumValues.Contains(str); + } } } From af9a31f4ad0d4f560526eee2fea2400563d7194e Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Tue, 9 Apr 2019 21:20:56 -0400 Subject: [PATCH 5/7] Keen broke network intercept. Disable it until we decide what to do. --- Torch/Managers/NetworkManager/NetworkManager_Deprecated.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Torch/Managers/NetworkManager/NetworkManager_Deprecated.cs b/Torch/Managers/NetworkManager/NetworkManager_Deprecated.cs index 5bfa3cc..1c2c580 100644 --- a/Torch/Managers/NetworkManager/NetworkManager_Deprecated.cs +++ b/Torch/Managers/NetworkManager/NetworkManager_Deprecated.cs @@ -68,6 +68,10 @@ namespace Torch.Managers /// public override void Attach() { + //disable all this for now + _log.Warn("Network intercept disabled. Some plugins may not work correctly."); + return; + if (_init) return; @@ -362,6 +366,9 @@ namespace Torch.Managers /// public void RegisterNetworkHandler(INetworkHandler handler) { + _log.Warn($"Plugin {handler.GetType().Assembly.FullName} registered a network handler. This system no longer works. Please alert the plugin author."); + return; + var handlerType = handler.GetType().FullName; var toRemove = new List(); foreach (var item in _networkHandlers) From b37cd74e56d53721d8ea8f7dc80a8a66933d2883 Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Wed, 10 Apr 2019 14:28:13 -0400 Subject: [PATCH 6/7] Fix gross initializer error on first time installations --- Torch/Utils/StringUtils.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Torch/Utils/StringUtils.cs b/Torch/Utils/StringUtils.cs index 167807a..2177009 100644 --- a/Torch/Utils/StringUtils.cs +++ b/Torch/Utils/StringUtils.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; -using VRage.Game; namespace Torch.Utils { @@ -63,10 +62,12 @@ namespace Torch.Utils return builder?.ToString() ?? ""; } - private static readonly string[] _fontEnumValues = typeof(MyFontEnum).GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => x.IsLiteral && !x.IsInitOnly).Select(x => (string)x.GetValue(null)).ToArray(); + private static string[] FontEnumValues => _fontEnumValues ?? (_fontEnumValues = typeof(VRage.Game.MyFontEnum).GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => x.IsLiteral && !x.IsInitOnly).Select(x => (string)x.GetValue(null)).ToArray()); + + private static string[] _fontEnumValues; public static bool IsFontEnum(string str) { - return _fontEnumValues.Contains(str); + return FontEnumValues.Contains(str); } } } From 5c8a1f36771243b12fd0dc8c47266ea52071de1c Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Wed, 10 Apr 2019 16:29:41 -0400 Subject: [PATCH 7/7] Don't throw exceptions when saving is abnormal --- Torch/Patches/TorchAsyncSaving.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Torch/Patches/TorchAsyncSaving.cs b/Torch/Patches/TorchAsyncSaving.cs index c20c59d..3c99e77 100644 --- a/Torch/Patches/TorchAsyncSaving.cs +++ b/Torch/Patches/TorchAsyncSaving.cs @@ -66,7 +66,7 @@ namespace Torch.Patches { if (!Game.IsDedicated && MySession.Static != null) ShowWorldSaveResult(tmpSnapshot.SavingSuccess); - saveTaskSource.SetResult(tmpSnapshot.SavingSuccess ? GameSaveResult.Success : GameSaveResult.FailedToSaveToDisk); + saveTaskSource.TrySetResult(tmpSnapshot.SavingSuccess ? GameSaveResult.Success : GameSaveResult.FailedToSaveToDisk); }); }); return saveTaskSource.Task;