Skip to content

Commit

Permalink
Add i18n command mitigation for Pryaxis/TShock#2914
Browse files Browse the repository at this point in the history
  • Loading branch information
sgkoishi committed Apr 29, 2023
1 parent b6bc07f commit 0a8e02f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class Config
Consts.Commands.DebugStat,
Consts.Commands.ResetCharacter,
Consts.Commands.Ping,
Consts.Commands.Chat,
Consts.Commands.Echo,
};

public List<string> StartupCommands = new List<string> { };
public List<string> StartupCommands = new List<string>();

public Dictionary<string, List<string>> CommandRenames = new();

Expand Down Expand Up @@ -494,6 +496,25 @@ public class MitigationSettings
/// </summary>
public bool KeepRestAlive = 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>
/// </summary>
public bool UseEnglishCommand = true;

[JsonConverter(typeof(StringEnumConverter))]
public enum DisabledDamageAction
{
Expand Down
19 changes: 19 additions & 0 deletions src/Mitigations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,23 @@ private void ILHook_Mitigation_KeepRestAlive(ILContext context)
}
}
}

private void Detour_Mitigation_I18nCommand(On.Terraria.Initializers.ChatInitializer.orig_Load orig)
{
// Pryaxis/TShock#2914
Terraria.UI.Chat.ChatManager.Commands._localizedCommands.Clear();
orig();
if (this.config.Mitigation.Enabled && this.config.Mitigation.UseEnglishCommand)
{
var currentLanguage = Terraria.Localization.LanguageManager.Instance.ActiveCulture;
Terraria.Localization.LanguageManager.Instance.LoadLanguage(Terraria.Localization.GameCulture.FromCultureName(Terraria.Localization.GameCulture.CultureName.English));
var items = Terraria.UI.Chat.ChatManager.Commands._localizedCommands.ToList();
Terraria.UI.Chat.ChatManager.Commands._localizedCommands.Clear();
foreach (var (key, value) in items)
{
Terraria.UI.Chat.ChatManager.Commands._localizedCommands[new Terraria.Localization.LocalizedText(key.Key, key.Value)] = value;
}
Terraria.Localization.LanguageManager.Instance.LoadLanguage(currentLanguage);
}
}
}
6 changes: 6 additions & 0 deletions src/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public Plugin(Main game) : base(game)
typeof(TShockAPI.Commands)
.GetMethod(nameof(TShockAPI.Commands.HandleCommand), _bfany)!,
this.Detour_Command_Alternative);
this.Detour(
nameof(this.Detour_Mitigation_I18nCommand),
typeof(Terraria.Initializers.ChatInitializer)
.GetMethod(nameof(Terraria.Initializers.ChatInitializer.Load), _bfany)!,
this.Detour_Mitigation_I18nCommand);
this.ILHook(
nameof(this.ILHook_Mitigation_DisabledInvincible),
Utils.TShockType("Bouncer")
Expand Down Expand Up @@ -186,6 +191,7 @@ private void OnReload(ReloadEventArgs? e)
{
TShockAPI.Commands.HandleCommand(TShockAPI.TSPlayer.Server, command);
}
Terraria.Initializers.ChatInitializer.Load();
}

public override void Initialize()
Expand Down

0 comments on commit 0a8e02f

Please sign in to comment.