Skip to content

Commit

Permalink
Allow localized vanilla command for internal call
Browse files Browse the repository at this point in the history
  • Loading branch information
sgkoishi committed Dec 19, 2023
1 parent 76c0e9f commit bb590d5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 45 deletions.
73 changes: 45 additions & 28 deletions Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,48 @@ public record class SoundnessSettings
/// Restrict sign edit to have build permission.
/// </summary>
public Optional<bool> SignEditRestriction = Optional.Default(true);

/// <summary>
/// <para>
/// Might experience encoding issues when using legacy Windows.
/// </para>
/// <para>
/// This will try to reset the encoding.
/// Default: -1, use Encoding.Default when Win32NT && Version <= 10
/// </para>
/// </summary>
public Optional<int> UseDefaultEncoding = Optional.Default(Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major < 10 ? -1 : 0);

/// <summary>
/// <para>
/// Terraria will translate chat commands into command id. TShock
/// translate them back to keep the command working.
/// However, when the server and the client have different locale,
/// a enUS player send `/help` will be sent as `CommandId: Help`
/// and a deDE server will translate it back to `/hilfe`, thus the
/// command is broken.
/// </para>
/// <para>Cause some commands broken.</para>
/// <para>
/// This will try to change the translate target to enUS, so that
/// the command will be translated back to `/help`. A deDE player
/// may run `/help` (CommandId: Say, Content: /help) or
/// `/hilfe` (CommandId: Help), and both works.
/// </para>
/// <see href="https://github.com/Pryaxis/TShock/issues/2914"/>
/// </summary>
public Optional<bool> UseEnglishCommand = Optional.Default(true, true);

/// <summary>
/// <para>
/// Accept vanilla localized command, which is already done via <see cref="UseEnglishCommand"/> if the
/// command comes from the client. However, the localized commands from the server side is still broken.
/// </para>
/// <para>
/// This will add alias for related commands. Requires <see cref="UseEnglishCommand"/>.
/// </para>
/// </summary>
public Optional<bool> AllowVanillaLocalizedCommand = Optional.Default(true, true);
}

public record class PermissionSettings
Expand Down Expand Up @@ -546,26 +588,6 @@ public record class MitigationSettings
/// </summary>
public Optional<bool> KeepRestAlive = Optional.Default(true, true);

/// <summary>
/// <para>
/// Terraria will translate chat commands into command id. TShock
/// translate them back to keep the command working.
/// However, when the server and the client have different locale,
/// a enUS player send `/help` will be sent as `CommandId: Help`
/// and a deDE server will translate it back to `/hilfe`, thus the
/// command is broken.
/// </para>
/// <para>Cause some commands broken.</para>
/// <para>
/// This will try to change the translate target to enUS, so that
/// the command will be translated back to `/help`. A deDE player
/// may run `/help` (CommandId: Say, Content: /help) or
/// `/hilfe` (CommandId: Help), and both works.
/// </para>
/// <see href="https://github.com/Pryaxis/TShock/issues/2914"/>
/// </summary>
public Optional<bool> UseEnglishCommand = Optional.Default(true, true);

/// <summary>
/// <para>
/// TShock update legacy config { "key1": "value1", "key2": "value2" } to
Expand Down Expand Up @@ -623,18 +645,13 @@ public record class MitigationSettings

/// <summary>
/// <para>
/// Might experience encoding issues when using legacy Windows.
/// Allow journey and non-journey players to join the server.
/// </para>
/// <para>
/// This will try to reset the encoding.
/// Default: -1, use Encoding.Default when Win32NT && Version <= 10
/// There is nothing wrong with this option. It is inside the Mitigations because it
/// requires GetData detour like many other mitigations and I put them together.
/// </para>
/// </summary>
public Optional<int> UseDefaultEncoding = Optional.Default(Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major < 10 ? -1 : 0);

/// <summary>
/// Allow journey and non-journey players to join the server.
/// </summary>
public Optional<bool> AllowCrossJourney = Optional.Default(false);

public enum DisabledDamageAction
Expand Down
17 changes: 15 additions & 2 deletions Core/Mitigations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,13 @@ private void ILHook_Mitigation_KeepRestAlive(ILContext context)
}
}

internal Dictionary<string, string> _localizedCommandsMap = [];
private void MMHook_Mitigation_I18nCommand(On.Terraria.Initializers.ChatInitializer.orig_Load orig)
{
// Pryaxis/TShock#2914
Terraria.UI.Chat.ChatManager.Commands._localizedCommands.Clear();
orig();
var mitigation = this.config.Mitigation.Value;
if (!mitigation.DisableAllMitigation && mitigation.UseEnglishCommand)
if (this.config.Soundness.Value.UseEnglishCommand)
{
var currentLanguage = Terraria.Localization.LanguageManager.Instance.ActiveCulture;
Terraria.Localization.LanguageManager.Instance.LoadLanguage(Terraria.Localization.GameCulture.FromCultureName(Terraria.Localization.GameCulture.CultureName.English));
Expand All @@ -523,7 +523,20 @@ private void MMHook_Mitigation_I18nCommand(On.Terraria.Initializers.ChatInitiali
{
Terraria.UI.Chat.ChatManager.Commands._localizedCommands[new Terraria.Localization.LocalizedText(key.Key, key.Value)] = value;
}
var chatCommands = items.ToDictionary(kvp => kvp.Key.Key, kvp => kvp.Key.Value);
var cliCommands = Terraria.Localization.LanguageManager.Instance._localizedTexts
.Where(kvp => kvp.Key.StartsWith("CLI.") && kvp.Key.EndsWith("_Command"))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Value);
Terraria.Localization.LanguageManager.Instance.LoadLanguage(currentLanguage);
foreach (var c in chatCommands)
{
// Chat commands start with / and we don't want that
this._localizedCommandsMap[Terraria.Localization.Language.GetText(c.Key).Value[1..]] = c.Value[1..];
}
foreach (var c in cliCommands)
{
this._localizedCommandsMap[Terraria.Localization.Language.GetText(c.Key).Value] = c.Value;
}
}
}

Expand Down
39 changes: 24 additions & 15 deletions Core/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,19 @@ public Plugin(Main game) : base(game)
}

{
var mitigation = this.config.Mitigation.Value;
if (!mitigation.DisableAllMitigation)
var encoding = this.config.Soundness.Value.UseDefaultEncoding.Value;
if (encoding != 0)
{
var encoding = mitigation.UseDefaultEncoding.Value;
if (encoding != 0)
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
if (encoding == -1)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
if (encoding == -1)
{
Console.OutputEncoding = System.Text.Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage);
Console.WriteLine($"Console encoding set to default ({Console.OutputEncoding.EncodingName})");
}
else
{
Console.OutputEncoding = System.Text.Encoding.GetEncoding(encoding);
Console.WriteLine($"Console encoding set to {Console.OutputEncoding.EncodingName}");
}
Console.OutputEncoding = System.Text.Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage);
Console.WriteLine($"Console encoding set to default ({Console.OutputEncoding.EncodingName})");
}
else
{
Console.OutputEncoding = System.Text.Encoding.GetEncoding(encoding);
Console.WriteLine($"Console encoding set to {Console.OutputEncoding.EncodingName}");
}
}
}
Expand Down Expand Up @@ -320,6 +316,19 @@ private void OnGamePostInitialize(EventArgs args)
On.Terraria.MessageBuffer.GetData += this.MMHook_DebugPacket_GetData;
On.Terraria.NetMessage.SendData += this.MMHook_DebugPacket_CatchSend;
On.Terraria.MessageBuffer.GetData += this.MMHook_DebugPacket_CatchGet;
if (this.config.Soundness.Value.AllowVanillaLocalizedCommand)
{
foreach (var command in TShockAPI.Commands.ChatCommands)
{
foreach (var bc in this._localizedCommandsMap)
{
if (command.HasAlias(bc.Value))
{
command.Names.Add(bc.Key);
}
}
}
}
}

private void PostTShockInitialize()
Expand Down

0 comments on commit bb590d5

Please sign in to comment.