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.Game.Gui;
using Sandbox.Game.Multiplayer;
using Torch.Utils;
using VRage.Game;
using VRage.Network;
using VRage.Replication;
using VRageMath;
using VRageRender;
namespace Torch.API.Managers
{
@@ -17,13 +20,31 @@ namespace Torch.API.Managers
/// </summary>
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>
/// Creates a new torch chat message with the given author and message.
/// </summary>
/// <param name="author">Author's name</param>
/// <param name="message">Message</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;
AuthorSteamId = null;
@@ -32,6 +53,7 @@ namespace Torch.API.Managers
Channel = ChatChannel.Global;
Target = 0;
Font = font;
Color = color == default ? ColorUtils.TranslateColor(font) : color;
}
/// <summary>
@@ -41,7 +63,7 @@ namespace Torch.API.Managers
/// <param name="authorSteamId">Author's steam ID</param>
/// <param name="message">Message</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;
AuthorSteamId = authorSteamId;
@@ -50,6 +72,7 @@ namespace Torch.API.Managers
Channel = channel;
Target = target;
Font = font;
Color = color == default ? ColorUtils.TranslateColor(font) : color;
}
/// <summary>
@@ -58,7 +81,7 @@ namespace Torch.API.Managers
/// <param name="authorSteamId">Author's steam ID</param>
/// <param name="message">Message</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;
AuthorSteamId = authorSteamId;
@@ -67,6 +90,7 @@ namespace Torch.API.Managers
Channel = channel;
Target = target;
Font = font;
Color = color == default ? ColorUtils.TranslateColor(font) : color;
}
/// <summary>
@@ -97,6 +121,10 @@ namespace Torch.API.Managers
/// The font, or null if default.
/// </summary>
public readonly string Font;
/// <summary>
/// The chat message color.
/// </summary>
public readonly Color Color;
}
/// <summary>

View File

@@ -4,7 +4,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage.Collections;
using VRage.Game;
using VRage.Network;
using VRageMath;
namespace Torch.API.Managers
{
@@ -34,14 +36,18 @@ namespace Torch.API.Managers
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>
/// Sends a scripted message with the given author and message to the given player, or all players by default.
/// </summary>
/// <param name="author">Author name</param>
/// <param name="message">The message to send</param>
/// <param name="color">Name color</param>
/// <param name="font">Font to use</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>
/// Mute user from global chat.

View File

@@ -12,6 +12,7 @@
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
@@ -193,6 +194,8 @@
<Compile Include="Session\ITorchSessionManager.cs" />
<Compile Include="Session\TorchSessionState.cs" />
<Compile Include="TorchGameState.cs" />
<Compile Include="Utils\ColorUtils.cs" />
<Compile Include="Utils\StringUtils.cs" />
<Compile Include="WebAPI\JenkinsQuery.cs" />
<Compile Include="WebAPI\PluginQuery.cs" />
</ItemGroup>
@@ -202,7 +205,6 @@
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\TransformOnBuild.targets" />
</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.Linq;
using System.Net;
using System.Text.RegularExpressions;
using Sandbox.Engine.Networking;
using Sandbox.Game.Multiplayer;
using Torch.API;
using Torch.API.Managers;
using Torch.API.Plugins;
using Torch.Utils;
using VRage.Game;
using VRage.Game.ModAPI;
using VRageMath;
namespace Torch.Commands
{
@@ -55,6 +58,19 @@ namespace Torch.Commands
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)
{
//hack: Backwards compatibility 20190416
@@ -64,8 +80,7 @@ namespace Torch.Commands
font = null;
}
var chat = Torch.CurrentSession.Managers.GetManager<IChatManagerServer>();
chat?.SendMessageAsOther(sender, message, font, _steamIdSender);
Respond(message, default, sender, font);
}
}
}

View File

@@ -10,6 +10,7 @@ using Torch.API;
using Torch.API.Managers;
using Torch.API.Plugins;
using Torch.Managers;
using Torch.Utils;
using VRage.Game;
using VRage.Game.ModAPI;
using VRage.Network;
@@ -130,7 +131,7 @@ namespace Torch.Commands
if (!HasPermission(steamId, command))
{
_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;
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using NLog;
using Sandbox.Engine.Multiplayer;
using Sandbox.Engine.Networking;
@@ -11,11 +12,13 @@ using Sandbox.Game.Gui;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.World;
using Sandbox.ModAPI;
using SteamKit2.Unified.Internal;
using Torch.API;
using Torch.API.Managers;
using Torch.Utils;
using VRage.Game;
using VRageMath;
using Color = VRageMath.Color;
namespace Torch.Managers.ChatManager
{
@@ -39,21 +42,21 @@ 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 = Torch.Config.ChatName,
Font = color,
Text = message,
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);
}
else

View File

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

View File

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