Add support for new chat colors (#379)

* Add backwards-compatible support for new chat color system

* Use default

* Set LangVersion properly

* Add backwards compatible ctors in TorchChatMessage

* Fix not setting Font if it's not MyFontEnum

* Use correct color mappings
This commit is contained in:
Jimmacle
2020-04-13 04:44:12 -07:00
committed by GitHub
parent 76ea5fdbc1
commit 1dd444759b
10 changed files with 127 additions and 22 deletions

View File

@@ -6,9 +6,12 @@ using System.Threading.Tasks;
using Sandbox.Engine.Multiplayer; using Sandbox.Engine.Multiplayer;
using Sandbox.Game.Gui; using Sandbox.Game.Gui;
using Sandbox.Game.Multiplayer; using Sandbox.Game.Multiplayer;
using Torch.Utils;
using VRage.Game; using VRage.Game;
using VRage.Network; using VRage.Network;
using VRage.Replication; using VRage.Replication;
using VRageMath;
using VRageRender;
namespace Torch.API.Managers namespace Torch.API.Managers
{ {
@@ -17,13 +20,31 @@ namespace Torch.API.Managers
/// </summary> /// </summary>
public struct TorchChatMessage public struct TorchChatMessage
{ {
private const string DEFAULT_FONT = MyFontEnum.Blue;
#region Backwards compatibility
[Obsolete]
public TorchChatMessage(string author, string message, string font = DEFAULT_FONT)
: this(author, message, default, font) { }
[Obsolete]
public TorchChatMessage(string author, ulong authorSteamId, string message, ChatChannel channel, long target, string font = DEFAULT_FONT)
: this(author, authorSteamId, message, channel, target, default, font) { }
[Obsolete]
public TorchChatMessage(ulong authorSteamId, string message, ChatChannel channel, long target, string font = DEFAULT_FONT)
: this(authorSteamId, message, channel, target, default, font) { }
#endregion
/// <summary> /// <summary>
/// Creates a new torch chat message with the given author and message. /// Creates a new torch chat message with the given author and message.
/// </summary> /// </summary>
/// <param name="author">Author's name</param> /// <param name="author">Author's name</param>
/// <param name="message">Message</param> /// <param name="message">Message</param>
/// <param name="font">Font</param> /// <param name="font">Font</param>
public TorchChatMessage(string author, string message, string font = MyFontEnum.Blue) public TorchChatMessage(string author, string message, Color color, string font = DEFAULT_FONT)
{ {
Timestamp = DateTime.Now; Timestamp = DateTime.Now;
AuthorSteamId = null; AuthorSteamId = null;
@@ -32,6 +53,7 @@ namespace Torch.API.Managers
Channel = ChatChannel.Global; Channel = ChatChannel.Global;
Target = 0; Target = 0;
Font = font; Font = font;
Color = color == default ? ColorUtils.TranslateColor(font) : color;
} }
/// <summary> /// <summary>
@@ -41,7 +63,7 @@ namespace Torch.API.Managers
/// <param name="authorSteamId">Author's steam ID</param> /// <param name="authorSteamId">Author's steam ID</param>
/// <param name="message">Message</param> /// <param name="message">Message</param>
/// <param name="font">Font</param> /// <param name="font">Font</param>
public TorchChatMessage(string author, ulong authorSteamId, string message, ChatChannel channel, long target, string font = MyFontEnum.Blue) public TorchChatMessage(string author, ulong authorSteamId, string message, ChatChannel channel, long target, Color color, string font = DEFAULT_FONT)
{ {
Timestamp = DateTime.Now; Timestamp = DateTime.Now;
AuthorSteamId = authorSteamId; AuthorSteamId = authorSteamId;
@@ -50,6 +72,7 @@ namespace Torch.API.Managers
Channel = channel; Channel = channel;
Target = target; Target = target;
Font = font; Font = font;
Color = color == default ? ColorUtils.TranslateColor(font) : color;
} }
/// <summary> /// <summary>
@@ -58,7 +81,7 @@ namespace Torch.API.Managers
/// <param name="authorSteamId">Author's steam ID</param> /// <param name="authorSteamId">Author's steam ID</param>
/// <param name="message">Message</param> /// <param name="message">Message</param>
/// <param name="font">Font</param> /// <param name="font">Font</param>
public TorchChatMessage(ulong authorSteamId, string message, ChatChannel channel, long target, string font = MyFontEnum.Blue) public TorchChatMessage(ulong authorSteamId, string message, ChatChannel channel, long target, Color color, string font = DEFAULT_FONT)
{ {
Timestamp = DateTime.Now; Timestamp = DateTime.Now;
AuthorSteamId = authorSteamId; AuthorSteamId = authorSteamId;
@@ -67,6 +90,7 @@ namespace Torch.API.Managers
Channel = channel; Channel = channel;
Target = target; Target = target;
Font = font; Font = font;
Color = color == default ? ColorUtils.TranslateColor(font) : color;
} }
/// <summary> /// <summary>
@@ -97,6 +121,10 @@ namespace Torch.API.Managers
/// The font, or null if default. /// The font, or null if default.
/// </summary> /// </summary>
public readonly string Font; public readonly string Font;
/// <summary>
/// The chat message color.
/// </summary>
public readonly Color Color;
} }
/// <summary> /// <summary>

View File

@@ -4,7 +4,9 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using VRage.Collections; using VRage.Collections;
using VRage.Game;
using VRage.Network; using VRage.Network;
using VRageMath;
namespace Torch.API.Managers namespace Torch.API.Managers
{ {
@@ -34,14 +36,18 @@ namespace Torch.API.Managers
void SendMessageAsOther(ulong authorId, string message, ulong targetSteamId = 0); void SendMessageAsOther(ulong authorId, string message, ulong targetSteamId = 0);
[Obsolete("Use the other overload with a Color parameter.")]
void SendMessageAsOther(string author, string message, string font, ulong targetSteamId = 0);
/// <summary> /// <summary>
/// Sends a scripted message with the given author and message to the given player, or all players by default. /// Sends a scripted message with the given author and message to the given player, or all players by default.
/// </summary> /// </summary>
/// <param name="author">Author name</param> /// <param name="author">Author name</param>
/// <param name="message">The message to send</param> /// <param name="message">The message to send</param>
/// <param name="color">Name color</param>
/// <param name="font">Font to use</param> /// <param name="font">Font to use</param>
/// <param name="targetSteamId">Player to send the message to, or everyone by default</param> /// <param name="targetSteamId">Player to send the message to, or everyone by default</param>
void SendMessageAsOther(string author, string message, string font, ulong targetSteamId = 0); void SendMessageAsOther(string author, string message, Color color = default, ulong targetSteamId = 0, string font = MyFontEnum.White);
/// <summary> /// <summary>
/// Mute user from global chat. /// Mute user from global chat.

View File

@@ -12,6 +12,7 @@
<TargetFrameworkProfile /> <TargetFrameworkProfile />
<NuGetPackageImportStamp> <NuGetPackageImportStamp>
</NuGetPackageImportStamp> </NuGetPackageImportStamp>
<LangVersion>7.3</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@@ -193,6 +194,8 @@
<Compile Include="Session\ITorchSessionManager.cs" /> <Compile Include="Session\ITorchSessionManager.cs" />
<Compile Include="Session\TorchSessionState.cs" /> <Compile Include="Session\TorchSessionState.cs" />
<Compile Include="TorchGameState.cs" /> <Compile Include="TorchGameState.cs" />
<Compile Include="Utils\ColorUtils.cs" />
<Compile Include="Utils\StringUtils.cs" />
<Compile Include="WebAPI\JenkinsQuery.cs" /> <Compile Include="WebAPI\JenkinsQuery.cs" />
<Compile Include="WebAPI\PluginQuery.cs" /> <Compile Include="WebAPI\PluginQuery.cs" />
</ItemGroup> </ItemGroup>
@@ -202,7 +205,6 @@
<ItemGroup> <ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" /> <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\TransformOnBuild.targets" /> <Import Project="$(SolutionDir)\TransformOnBuild.targets" />
</Project> </Project>

View File

@@ -0,0 +1,40 @@
using System.Windows.Media;
using VRage.Game;
using Color = VRageMath.Color;
namespace Torch.Utils
{
public static class ColorUtils
{
/// <summary>
/// Convert the old "font" or a RGB hex code to a Color.
/// </summary>
public static Color TranslateColor(string font)
{
if (StringUtils.IsFontEnum(font))
{
// RGB values copied from Fonts.sbc
switch (font)
{
case MyFontEnum.Blue:
return new Color(220, 244, 252);
case MyFontEnum.Red:
return new Color(227, 65, 65);
case MyFontEnum.Green:
return new Color(101, 182, 193);
case MyFontEnum.DarkBlue:
return new Color(94, 115, 127);
default:
return Color.White;
}
}
else
{
// VRage color doesn't have its own hex code parser and I don't want to write one
var conv = (System.Windows.Media.Color)(ColorConverter.ConvertFromString(font) ??
System.Windows.Media.Color.FromRgb(255, 255, 255));
return new Color(conv.R, conv.G, conv.B);
}
}
}
}

View File

@@ -1,13 +1,16 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Sandbox.Engine.Networking; using Sandbox.Engine.Networking;
using Sandbox.Game.Multiplayer; using Sandbox.Game.Multiplayer;
using Torch.API; using Torch.API;
using Torch.API.Managers; using Torch.API.Managers;
using Torch.API.Plugins; using Torch.API.Plugins;
using Torch.Utils;
using VRage.Game; using VRage.Game;
using VRage.Game.ModAPI; using VRage.Game.ModAPI;
using VRageMath;
namespace Torch.Commands namespace Torch.Commands
{ {
@@ -55,6 +58,19 @@ namespace Torch.Commands
Args = args ?? new List<string>(); Args = args ?? new List<string>();
} }
public void Respond(string message, Color color, string sender = null, string font = null)
{
var chat = Torch.CurrentSession.Managers.GetManager<IChatManagerServer>();
if (color == default && font != null)
color = ColorUtils.TranslateColor(font);
if (font == null)
font = MyFontEnum.White;
chat?.SendMessageAsOther(sender, message, color, font: font);
}
public virtual void Respond(string message, string sender = null, string font = null) public virtual void Respond(string message, string sender = null, string font = null)
{ {
//hack: Backwards compatibility 20190416 //hack: Backwards compatibility 20190416
@@ -64,8 +80,7 @@ namespace Torch.Commands
font = null; font = null;
} }
var chat = Torch.CurrentSession.Managers.GetManager<IChatManagerServer>(); Respond(message, default, sender, font);
chat?.SendMessageAsOther(sender, message, font, _steamIdSender);
} }
} }
} }

View File

@@ -10,6 +10,7 @@ using Torch.API;
using Torch.API.Managers; using Torch.API.Managers;
using Torch.API.Plugins; using Torch.API.Plugins;
using Torch.Managers; using Torch.Managers;
using Torch.Utils;
using VRage.Game; using VRage.Game;
using VRage.Game.ModAPI; using VRage.Game.ModAPI;
using VRage.Network; using VRage.Network;
@@ -130,7 +131,7 @@ namespace Torch.Commands
if (!HasPermission(steamId, command)) if (!HasPermission(steamId, command))
{ {
_log.Info($"{player.DisplayName} tried to use command {cmdPath} without permission"); _log.Info($"{player.DisplayName} tried to use command {cmdPath} without permission");
_chatManager.SendMessageAsOther(Torch.Config.ChatName, $"You need to be a {command.MinimumPromoteLevel} or higher to use that command.", Torch.Config.ChatColor, steamId); _chatManager.SendMessageAsOther(null, $"You need to be a {command.MinimumPromoteLevel} or higher to use that command.", targetSteamId: steamId);
return; return;
} }

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Media;
using NLog; using NLog;
using Sandbox.Engine.Multiplayer; using Sandbox.Engine.Multiplayer;
using Sandbox.Engine.Networking; using Sandbox.Engine.Networking;
@@ -11,11 +12,13 @@ using Sandbox.Game.Gui;
using Sandbox.Game.Multiplayer; using Sandbox.Game.Multiplayer;
using Sandbox.Game.World; using Sandbox.Game.World;
using Sandbox.ModAPI; using Sandbox.ModAPI;
using SteamKit2.Unified.Internal;
using Torch.API; using Torch.API;
using Torch.API.Managers; using Torch.API.Managers;
using Torch.Utils; using Torch.Utils;
using VRage.Game; using VRage.Game;
using VRageMath; using VRageMath;
using Color = VRageMath.Color;
namespace Torch.Managers.ChatManager namespace Torch.Managers.ChatManager
{ {
@@ -39,21 +42,21 @@ namespace Torch.Managers.ChatManager
{ {
if (Sandbox.Engine.Platform.Game.IsDedicated) 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() var scripted = new ScriptedChatMsg()
{ {
Author = Torch.Config.ChatName, Author = Torch.Config.ChatName,
Font = color,
Text = message, Text = message,
Target = 0 Target = 0
}; };
var color = Torch.Config.ChatColor;
if (StringUtils.IsFontEnum(color))
scripted.Font = color;
else
scripted.Font = MyFontEnum.White;
scripted.Color = ColorUtils.TranslateColor(color);
MyMultiplayerBase.SendScriptedChatMessage(ref scripted); MyMultiplayerBase.SendScriptedChatMessage(ref scripted);
} }
else else

View File

@@ -17,8 +17,10 @@ using Torch.Managers.PatchManager;
using Torch.Utils; using Torch.Utils;
using VRage; using VRage;
using VRage.Collections; using VRage.Collections;
using VRage.Game;
using VRage.Library.Collections; using VRage.Library.Collections;
using VRage.Network; using VRage.Network;
using VRageMath;
namespace Torch.Managers.ChatManager namespace Torch.Managers.ChatManager
{ {
@@ -107,8 +109,7 @@ namespace Torch.Managers.ChatManager
private static MethodInfo _dedicatedServerBaseOnChatMessage; private static MethodInfo _dedicatedServerBaseOnChatMessage;
#pragma warning restore 649 #pragma warning restore 649
/// <inheritdoc /> public void SendMessageAsOther(string author, string message, Color color = default, ulong targetSteamId = 0, string font = MyFontEnum.White)
public void SendMessageAsOther(string author, string message, string font, ulong targetSteamId = 0)
{ {
if (targetSteamId == Sync.MyId) if (targetSteamId == Sync.MyId)
{ {
@@ -125,13 +126,23 @@ namespace Torch.Managers.ChatManager
{ {
Author = author ?? Torch.Config.ChatName, Author = author ?? Torch.Config.ChatName,
Text = message, Text = message,
Font = font ?? Torch.Config.ChatColor, Font = font,
Color = color == default ? ColorUtils.TranslateColor(Torch.Config.ChatColor) : color,
Target = Sync.Players.TryGetIdentityId(targetSteamId) Target = Sync.Players.TryGetIdentityId(targetSteamId)
}; };
_chatLog.Info($"{author} (to {GetMemberName(targetSteamId)}): {message}"); _chatLog.Info($"{author} (to {GetMemberName(targetSteamId)}): {message}");
MyMultiplayerBase.SendScriptedChatMessage(ref scripted); MyMultiplayerBase.SendScriptedChatMessage(ref scripted);
} }
/// <summary>
/// Backwards compatibility
/// </summary>
[Obsolete("Use the other overload with a Color parameter.")]
public void SendMessageAsOther(string author, string message, string font, ulong targetSteamId = 0)
{
SendMessageAsOther(author, message, ColorUtils.TranslateColor(font), targetSteamId, font);
}
/// <inheritdoc /> /// <inheritdoc />
protected override bool OfflineMessageProcessor(TorchChatMessage msg) protected override bool OfflineMessageProcessor(TorchChatMessage msg)
{ {

View File

@@ -286,7 +286,6 @@
<Compile Include="Utils\Reflected\ReflectedStaticMethodAttribute.cs" /> <Compile Include="Utils\Reflected\ReflectedStaticMethodAttribute.cs" />
<Compile Include="Utils\Reflection.cs" /> <Compile Include="Utils\Reflection.cs" />
<Compile Include="Utils\SteamWorkshopTools\WebAPI.cs" /> <Compile Include="Utils\SteamWorkshopTools\WebAPI.cs" />
<Compile Include="Utils\StringUtils.cs" />
<Compile Include="Utils\SynchronizationExtensions.cs" /> <Compile Include="Utils\SynchronizationExtensions.cs" />
<Compile Include="Utils\TorchAssemblyResolver.cs" /> <Compile Include="Utils\TorchAssemblyResolver.cs" />
<Compile Include="Utils\Reflected\ReflectedManager.cs" /> <Compile Include="Utils\Reflected\ReflectedManager.cs" />